【问题标题】:Equivalence in GraphQL with { [key: string]: string } in TypeScript在 GraphQL 中与 TypeScript 中的 { [key: string]: string } 等价
【发布时间】:2021-12-31 21:14:24
【问题描述】:

TypeScript 中 GraphQL 中的 GraphQL { [key: string]: string } 等价是什么?

#schema.gql
type App implements Node {
  name: String!
  tags: 'EQUIVALENCE { [key: string]: string }'
}

谢谢!

【问题讨论】:

    标签: typescript graphql


    【解决方案1】:

    在 GraphQL 中不鼓励使用原始 JSON(这是您的 tags 字段的样子),请参阅此讨论:https://github.com/graphql/graphql-spec/issues/612

    但是,有一种方法可以使用自定义标量 (https://www.graphql-tools.com/docs/scalars) 并将 tags 键入为 JSON。见这里:https://www.graphql-tools.com/docs/scalars#using-a-package

    我在这里复制示例以供后代使用

    import { makeExecutableSchema } from '@graphql-tools/schema'
    import { GraphQLJSON } from 'graphql-scalars'
    
    const schemaString = `
    
    scalar JSON
    
    type Foo {
      tags: JSON
    }
    
    type Query {
      foo: Foo
    }
    
    `
    
    const resolveFunctions = {
      JSON: GraphQLJSON
    }
    
    const jsSchema = makeExecutableSchema({ typeDefs: schemaString, resolvers: resolveFunctions })
    

    如果可以的话,我建议选择这样的:

    #schema.gql
    type App implements Node {
      name: String!
      tags: [ { name: String!, value: String! }! ]!
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2020-07-05
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多