gql-generator
Generate queries from graphql schema, used for writing api test.
Example
# Sample schematype Query { user(id: Int!): User!} type User { id: Int! username: String! email: String! createdAt: String!}
# Sample query generatedquery user($id: Int!) { user(id: $id){ id username email createdAt }}
Usage
# Install npm install gql-generator -g # see the usage gqlg --help # Generate sample queries from schema file gqlg --schemaFilePath ./example/sampleTypeDef.graphql --destDirPath ./example/output --depthLimit 5
Now the queries generated from the sampleTypeDef.graphql
can be found in the destDir: ./example/output
.
This tool generate 3 folders holding the queries: mutations, queries and subscriptions. And also index.js
files to export the queries in each folder.
You can require the queries like this:
// require all the queriesconst queries = ;// require mutations onlyconst mutations = ; // sample contentconsole;console;/*mutation signup($username: String!, email: String!, password: String!){ signup(username: $username, email: $email, password: $password){ token user { id username email createdAt } }}*/
Usage example
Say you have a graphql schema like this:
type Mutation { signup( email: String! username: String! password: String! ): UserToken!} type UserToken { token: String! user: User!} type User { id: Int! username: String! email: String! createdAt: String!}
Before this tool, you write graphql api test like this:
const GraphQLClient = ;; const host = 'http://localhost:8080/graphql';
As gqlg
generated the queries for you, you don't need to write the query yourself, so your test will becomes:
const GraphQLClient = ;;const mutations = ; const host = 'http://localhost:8080/graphql';
Notice
As this tool is used for test, it expends all the fields in a query. And as we know, there might be recursive field in the query. So gqlg
ignores the types which has been added in the parent queries already.