[英]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?在重复模式参数的 Graphql 模式中使用 static 变量是一种好习惯吗? 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取自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
谢谢托马斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.