Giter Club home page Giter Club logo

gql-generator's Introduction

gql-generator

Generate queries from graphql schema, used for writing api test.

Usage

# Install
npm install gql-generator -g

# see the usage
gqlg --help

# Generate sample queries from typeDefs
gqlg --schemaFilePath ./example/sampleTypeDef.graphql --destDirPath ./example/output

Now you can find the generated queries in the destDir: ./example/output.

# 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
    }
}

This tool will generate 3 folders holding the queries: mutations, queries and subscriptions. And generate a index.js exports the queries in each folder.

Also another index.js is generated in the root destPath to export all the queries (you can look into the example folder for details of generated files).

After generating the queries, you can require them like this:

// require all the queries
const queries = require('./example/output');
// require mutations only
const mutations = require('./example/output/mutations');

// sampe 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
        }
    }
}
*/

Sample result:

The output folder inside example folder is generated from the sampleTypeDef.graphql by this command:

gqlg --schemaFilePath ./example/sampleTypeDef.graphql --destDirPath ./example/output

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 } = 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 gqlg generated the queries for you, you don't need to write the query yourself, so your test will becomes:

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');
);

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.

gql-generator's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.