【发布时间】: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
TypeScript 中 GraphQL 中的 GraphQL { [key: string]: string } 等价是什么?
#schema.gql
type App implements Node {
name: String!
tags: 'EQUIVALENCE { [key: string]: string }'
}
谢谢!
【问题讨论】:
标签: typescript graphql
在 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! }! ]!
}
【讨论】: