繁体   English   中英

在 Graphql 模式中使用 static 变量以避免重复?

[英]Using static variables in a Graphql schema to avoid duplication?

在重复模式参数的 Graphql 模式中使用 static 变量是一种好习惯吗? 如下例所示

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}
`;

我是否也可以在客户端采用类似的方法来再次避免重复?

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

...

        ... on TeaserGroupWorkflow {
          id
          headline
          description
          elementLayout

...

取自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
  }
`;

和.. :

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
      }
    }
  }
`;

这是一个很好的做法,所有片段都按预期工作。 谢谢托马斯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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