在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):timqian/gql-generator开源软件地址(OpenSource Url):https://github.com/timqian/gql-generator开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gql-generatorGenerate queries from graphql schema, used for writing api test. Example# Sample schema
type Query {
user(id: Int!): User!
}
type User {
id: Int!
username: String!
email: String!
createdAt: String!
} # Sample query generated
query 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 This tool generate 3 folders holding the queries: mutations, queries and subscriptions. And also You can require the queries like this: // require all the queries
const queries = require('./example/output');
// require mutations only
const mutations = require('./example/output/mutations');
// sample content
console.log(queries.mutations.signup);
console.log(mutations.signup);
/*
mutation signup($username: String!, email: String!, password: String!){
signup(username: $username, email: $email, password: $password){
token
user {
id
username
email
createdAt
}
}
}
*/ The tool will automatically exclude any Usage exampleSay 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 } = require('graphql-request');
require('should');
const host = 'http://localhost:8080/graphql';
test('signup', async () => {
const gql = new GraphQLClient(host);
const query = `mutation signup($username: String!, email: String!, password: String!){
signup(username: $username, email: $email, password: $password){
token
user {
id
username
email
createdAt
}
}
}`;
const data = await gql.request(query, {
username: 'tim',
email: '[email protected]',
password: 'samplepass',
});
(typeof data.signup.token).should.equal('string');
); As const { GraphQLClient } = require('graphql-request');
require('should');
const mutations = require('./example/output/mutations');
const host = 'http://localhost:8080/graphql';
test('signup', async () => {
const gql = new GraphQLClient(host);
const data = await gql.request(mutations.signup, {
username: 'tim',
email: '[email protected]',
password: 'samplepass',
});
(typeof data.signup.token).should.equal('string');
); Notes
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论