我有一个代码优先的graphQl API,其中创建了GraphQlSchema的创建:
import { GraphQLSchema } from 'graphql';
import { mutationType } from './mutation';
import { queryType } from './query';
const schema = new GraphQLSchema({
query: queryType,
mutation: mutationType,
});
export { schema };
我知道如何使用 printschema(schema)(schema)
生成架构定义语言(SDL)编写的架构。
现在,我需要一个SDK来为我的API客户提供。我的问题是,我找不到任何用于从模式中生成键入SDK的库。
我发现的图书馆喜欢 graphql-request , ureql 或让你写
整个查询,没有任何类型的语法检查。
import { gql, GraphQLClient } from 'graphql-request'
const query = gql` //
{ //
Movie(title: "Inception") { // I can write whatever I want
releaseDate // here and no transpilation
actors { // errors will appear.
name // I have no autocompletion
} // whatsoever.
} //
} //
`
const client = new GraphQLClient('https://api.graph.cool/simple/v1/movies', { headers: {} })
client.request(query, variables).then((data) => console.log(data))
我希望能够执行 const client = new GraphQlClient(端点,架构,选项);
之类的事情。然后像So 一样使用它,等待client.movie(title)({preamate:true,Actors:{name:true}});
。不一定使用该语法,但我希望这可以解决这个想法。如果我编写 rlearesate
,我只想在IDE中的红线下方一条红线,如果我运行 tsc
,则只需一个转介错误。
我已经找到 printschema(schema)生成的架构。但是,对于可以生成我想要的SDK的打字稿 - 格拉夫Ql-Request插件,看来我需要自己编写一个aperion.graphql文件,这是我要避免的第一件事。正如我所说,API首先是代码优先,并且一开始我谈论的架构中的所有内容。我不希望该操作中的第二个真相来源。graphql文件。
我想要的吗?
I have a code-first GraphQL API in which the GraphQLSchema is created like so:
import { GraphQLSchema } from 'graphql';
import { mutationType } from './mutation';
import { queryType } from './query';
const schema = new GraphQLSchema({
query: queryType,
mutation: mutationType,
});
export { schema };
I know how to generate a schema written in Schema Definition Language (SDL) with printSchema(schema)
.
Now I need a SDK to provide to clients of my API. My problem is that I can't find any library for generating a typed SDK from the schema.
The libraries I find like graphql-request, urql or apollo-client make you write
the whole query without any type of syntax check.
import { gql, GraphQLClient } from 'graphql-request'
const query = gql` //
{ //
Movie(title: "Inception") { // I can write whatever I want
releaseDate // here and no transpilation
actors { // errors will appear.
name // I have no autocompletion
} // whatsoever.
} //
} //
`
const client = new GraphQLClient('https://api.graph.cool/simple/v1/movies', { headers: {} })
client.request(query, variables).then((data) => console.log(data))
I want to be able to do something like const client = new GraphQLClient(endpoint, schema, options);
. And then use it like so await client.movie(title)({releaseDate: true, actors: {name: true}});
. Not necessarily with that syntax, but I hope this gets the idea across. If I write rleaseDate
I just want a red line under it in my IDE and a transpilation error if I run tsc
.
I've found graphql-code-generator and I see that it can generate different things using the schema generated with printSchema(schema)
. But for the typescript-graphql-request plugin that can generate the SDK I want, it appears I would need to write an operation.graphql file myself, which is the first thing I want to avoid. The API, as I said, is code-first and everything is in the schema I talk about in the start. I don't want a second source of truth in that operation.graphql file.
Does what I want exist?
发布评论
评论(1)
好吧!因此,我找到了我在寻找的东西: graphql-zeus
命令
[通往SDL文件的路径] - typescript -node
生成一个可以使用API的客户端提供的SDK,他们可以像这样使用SDK:操作
createuser
及其参数已键入。这意味着我会遇到转卸错误,而IDE会告诉我我可以在每个字段中输入什么。Alright! So I found what I was looking for: graphql-zeus
The command
zeus [path to SDL file] --typescript --node
generates an SDK I can provide to the clients that will use the API, and they can use the SDK like so:And
mutation
, the operationcreateUser
and it's parameters are typed. Meaning I will get transpilation errors and the IDE will tell me what I can type in each field.