无法在生成的模式中打印自定义指令。

huangapple go评论57阅读模式
英文:

Unable To Print a Custom Directive in a Generated Schema

问题

以下是您要求的翻译部分:

我正在尝试在运行时添加一个指令,该指令在输出模式中生成正确,但我需要该指令用于某些字段,但它没有显示出来。
以下是我创建指令的方式:

const limitDirective = new graphql.GraphQLDirective({
  name: 'myDirective',
  locations: [graphql.DirectiveLocation.INPUT_FIELD_DEFINITION, graphql.DirectiveLocation.FIELD_DEFINITION, graphql.DirectiveLocation.ARGUMENT_DEFINITION],
  args: {
    constraint: {
      type: graphql.GraphQLString,
      description: 'myDirective的示例参数',
    },
    errorCode: {
      type: graphql.GraphQLString,
      description: 'myDirective的另一个示例参数',
    },
  },
  isRepeatable: true,
});

以下是我如何将其集成到InputField中的扩展中:

let customInput: graphql.GraphQLInputObjectType = new graphql.GraphQLInputObjectType({
  name: 'customInput',
  fields: () => ({
    myField: { type: graphql.GraphQLInt, extensions: {
      myDirective:{constraint:"max=50", errorCode:"LIMIT_EXCEEDED"}
    }}
  })
})

然而,在打印带有指令的模式时,这不会在生成的模式中出现。
我正在使用graph-tools的以下函数[打印带有指令的模式][1]。

**注意:**指令本身在生成的文件中是生成的,但未添加到输入字段。这是问题所在。

**期望输出:**

directive @myDirective(
  constraint: String
  errorCode: String
) repeatable on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 

input customInput {
 myField: String @myDirective(constraint: "max=50", errorCode: "LIMIT_EXCEEDED")
}

**实际输出:**

directive @myDirective(
  constraint: String
  errorCode: String
) repeatable on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 

input customInput {
 myField: String
}

[1]: https://github.com/ardatan/graphql-tools/blob/master/packages/utils/src/print-schema-with-directives.ts

请注意,我只提供了您请求的翻译,没有包括问题的回答。

英文:

I am trying to add a directive at run time which is being generated correctly in the output schema, however I require the directive to be used on certain fields and it's not showing up.
Here is how I created the directive

const limitDirective = new graphql.GraphQLDirective({
name: 'myDirective',
locations: [graphql.DirectiveLocation.INPUT_FIELD_DEFINITION, graphql.DirectiveLocation.FIELD_DEFINITION, graphql.DirectiveLocation.ARGUMENT_DEFINITION],
args: {
    constraint: {
        type: graphql.GraphQLString,
        description: 'An example argument for myDirective',
    },
    errorCode: {
        type: graphql.GraphQLString,
        description: 'Another example argument for myDirective',
    },
},
isRepeatable: true,
});

Here is how I integrated it into the InputField using extensions:

 let customInput: graphql.GraphQLInputObjectType = new graphql.GraphQLInputObjectType({
        name: "customInput",
        fields: () => ({
            myField: { type: graphql.GraphQLInt, extensions: {
              myDirective:{constraint:"max=50", errorCode:"LIMIT_EXCEEDED"}
            }}
        })
    })

However when printing the schema with the directives this is not being generated in the resulting schema.
I am using the following function to print the schema with directives by graph-tools.

Note: The directive itself is being generated in the resultant file it's not being added to the input field. That's the issue.

Expected Output:

directive @myDirective(
  constraint: String
  errorCode: String
) repeatable on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 

input customInput {
 myField: String @myDirective(constraint: "max=50", errorCode: "LIMIT_EXCEEDED")
}

Actual Output:

directive @myDirective(
  constraint: String
  errorCode: String
) repeatable on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 

input customInput {
 myField: String
}

答案1

得分: 0

以下是翻译好的部分:

所以解决方案是使用来自 graphql-tools 的 printSchemaWithDirectives 函数。
该函数还接受一个用于 pathToDirectivesInExtensions 的参数。这意味着在输入的字段中,我们需要添加扩展。
在扩展中使用的路径(即 demoDirectives)与传递给 printSchemaWithDirectives 函数的路径相同。demoDirectives 对象可以具有任意数量的指令,以及如下定义的参数。

import { printSchemaWithDirectives, addTypes, makeDirectiveNode, GetDocumentNodeFromSchemaOptions } from '@graphql-tools/utils';
import * as graphql from 'graphql';

main()
export async function main() {
  const customDirective = new graphql.GraphQLDirective({ name: 'myDirective', locations: [graphql.DirectiveLocation.INPUT_FIELD_DEFINITION, graphql.DirectiveLocation.FIELD_DEFINITION], args: { constraint: { type: graphql.GraphQLString, description: 'An example argument for myDirective', }, errorCode: { type: graphql.GraphQLString, description: 'Another example argument for myDirective', }, }, isRepeatable: false, });
  let outputSchema =  new graphql.GraphQLSchema({ directives:[customDirective]})
  let customInput: graphql.GraphQLInputObjectType = new graphql.GraphQLInputObjectType({ name: 'customInput', fields: () => ({ myField: { type: graphql.GraphQLInt, extensions: { demoDirectives:{myDirective: {constraint:"abc"}} }}, }) })
  outputSchema = addTypes(outputSchema, [customInput])
  console.log(printSchemaWithDirectives(outputSchema,{pathToDirectivesInExtensions:['demoDirectives']}))
}

示例输出:

directive @myDirective(
  """An example argument for myDirective"""
  constraint: String
  """Another example argument for myDirective"""
  errorCode: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

input customInput {
  myField: Int @myDirective(constraint: "abc")
}
英文:

So the solution is using printSchemaWithDirectives from graphql-tools.
This function also takes in an argument for pathToDirectivesInExtensions. Which means in the fields of the input we need to add extensions.
The path used in the extension (i.e demoDirectives) is the same path to pass in the printSchemaWithDirectives function. The demoDirectives Object can have any number of directives. And their arguments defined as such

import { printSchemaWithDirectives, addTypes, makeDirectiveNode, GetDocumentNodeFromSchemaOptions } from '@graphql-tools/utils';
import * as graphql from 'graphql';

main()
export async function main() {
  const customDirective = new graphql.GraphQLDirective({ name: 'myDirective', locations: [graphql.DirectiveLocation.INPUT_FIELD_DEFINITION, graphql.DirectiveLocation.FIELD_DEFINITION], args: { constraint: { type: graphql.GraphQLString, description: 'An example argument for myDirective', }, errorCode: { type: graphql.GraphQLString, description: 'Another example argument for myDirective', }, }, isRepeatable: false, });
  let outputSchema =  new graphql.GraphQLSchema({ directives:[customDirective]})
  let customInput: graphql.GraphQLInputObjectType = new graphql.GraphQLInputObjectType({ name: 'customInput', fields: () => ({ myField: { type: graphql.GraphQLInt, extensions: { demoDirectives:{myDirective: {constraint:"abc"}} }}, }) })
  outputSchema = addTypes(outputSchema, [customInput])
  console.log(printSchemaWithDirectives(outputSchema,{pathToDirectivesInExtensions:['demoDirectives']}))
}

Sample Output:

directive @myDirective(
  """An example argument for myDirective"""
  constraint: String
  """Another example argument for myDirective"""
  errorCode: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

input customInput {
  myField: Int @myDirective(constraint: "abc")
}

huangapple
  • 本文由 发表于 2023年4月17日 21:37:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定