简体   繁体   中英

Using static variables in a Graphql schema to avoid duplication?

Is it a good practice to use static variables in a Graphql schema on repeated schema params? like shown in the example below

const TeaserGroupParams = `{
    id: String
    workflowId: String
    headline: String
    description: String
    groupLayout: TeaserGroupLayout
    elementLayout: TeaserGroupElementLayout
    teaserElements: [TeaserElement]
    teaserDynamic: TeaserDynamic
    teaserDynamicConfigId: Int
    toplistSorting: String
    bookmarked: Boolean
    workingCopy: Workflow
    workflows: [Workflow]
    assignmentCount: Int
    assignedElements: [AssignedUnion]
    }
`;

export const TeaserGroupSchema = `

  union AssignedUnion = Article | Page

  type TeaserGroup ${TeaserGroupParams}
`;

export const TeaserGroupWorkflowSchema = `
  type TeaserGroupWorkflow ${TeaserGroupParams}
`;

Can I also have a similar approach on the client-side, to avoid duplication once more?

query GetTeaserGroup ($id: String!) {
    teaserGroup(id: $id) {
      asset {
        ... on TeaserGroup {
          id
          headline

...

        ... on TeaserGroupWorkflow {
          id
          headline
          description
          elementLayout

...

Took from https://www.apollographql.com/docs/react/caching/cache-configuration

import { gql } from '@apollo/client';

export const CORE_COMMENT_FIELDS = gql`
  fragment CoreCommentFields on Comment {
    id
    postedBy {
      username
      displayName
    }
    createdAt
    content
  }
`;

and.. :

import { gql } from '@apollo/client';
import { CORE_COMMENT_FIELDS } from './fragments';

export const GET_POST_DETAILS = gql`
  ${CORE_COMMENT_FIELDS}
  query CommentsForPost($postId: ID!) {
    post(postId: $postId) {
      title
      body
      author
      comments {
        ...CoreCommentFields
      }
    }
  }
`;

It was a good practice, with Fragments all works as intended. Thank you Tomasz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM