0.0.32 • Published 5 years ago

azachii-gql-generator v0.0.32

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

gql-generator

Build a gql query easy and avoid typo and schema errors.

Installation

npm install --save-dev gql-generator

Required packages

npm install --save graphql graphql-tag

Usage

This package provides control of gql files to dynamic add fields, operations, args, etc.

GQLGenerator

These class save the path of files and the schema to further validations.

initialize

Here we have to tell the graphql endpoint and the path of the query and mutation files. If we don't pass the path of files, this package is going to create them with querys.js and mutations.js names.

ParamTypeKind
clientString or ApolloClientRequired
queryFileStringOptional
mutationFileStringOptional
import GQLGenerator from '@azachii/gql-generator';

await GQLGenerator.initialize(
  'http://localhost:3000',
  './lib/apollo/querys.js',
  './lib/apollo/mutations.js',
);

Files should look like this:

import gql from 'graphql-tag';

GQLBuilder

Here were going to find the operations to manipulate the files.

createQueryFile / createMutationFile

This is how we can manually create the files.

ParamTypeKind
fileNameStringRequired
import { GQLBuilder } from '@azachii/gql-generator';

GQLBuilder.createQueryFile('./lib/apollo/myQuerys.js');
GQLBuilder.createMutationFile('./lib/apollo/myMutations.js');

createQuery / createMutation

This is what we must call to add a query or mutation to our files.

ParamTypeKind
nameStringRequired
variablesObject or ArrayOptional
fieldsString, Object or ArrayOptional
  • variables:
    • Object:
      • name: String (Required)
      • type: String, must contain a valid GraphQLType (Required)
    • Array: must contain objects with same structure as above
  • fields:
    • String: name of the field
    • Object:
      • name: String (Required)
      • fields: Same structure as parent (Optional)
      • args:
        • String: name of arg, should have a variable name (Required)
        • Array: must contain objects with same structure as above
    • Array: must contain objects with same structure as above
import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.createQuery(
  'authQuery',
  null,
  [
    {
      name: 'currentUser',
      fields: {
        name: 'user',
        fields: [
          'name',
          'lastName',
        ],
      },
    },
  ],
);
await GQLBuilder.createMutation(
  'signUpUser',
  [
    {
      name: 'email',
      type: 'String!',
    },
    {
      name: 'password',
      type: 'String!',
    },
  ],
  {
    name: 'createUser',
    args: [
      'email',
      'password',
    ],
    fields: [
      'token',
      {
        name: 'user',
        fields: [
          'name',
          'lastName',
        ],
      },
    ],
  },
);

And the result would be:

import gql from 'graphql-tag';

const authQuery = gql`query authQuery { currentUser { user { name, lastName } } }`;

export { authQuery };
import gql from 'graphql-tag';

const signUpUser = gql`mutation signUpUser($email: String!, password: String!) { createUser(email: $email, password: $password) { token, user: { name, lastName } } }`;

export { signUpUser };

removeQuery / removeMutation

This allows us to erase a query or mutation.

ParamTypeKind
nameStringRequired
import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.removeQuery('authQuery');
await GQLBuilder.removeMutation('signUpUser');

addQueryField / addMutationField

If we want to add a field to a query or mutation, this is the way.

ParamTypeKind
operationNameStringRequired
fieldString or ObjectRequired
  • field:
    • String: field name
    • Object:
      • name: String (Required)
      • inside: Array of strings, the fields where the field is inside of
import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.addQueryField(
  'authQuery',
  {
    name: 'email',
    inside: [
      'currentUser',
      'user'
    ]
  }
);
await GQLBuilder.addMutationField(
  'signUpUser',
  {
    name: 'email',
    inside: [
      'createUser',
      'user'
    ],
  }
);

removeQueryField / removeMutationField

This is the way to remove a field from a query or mutation.

ParamTypeKind
operationNameStringRequired

|field|String or Object|Required|

import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.removeQueryField(
  'authQuery',
  {
    name: 'lastName',
    inside: [
      'currentUser',
      'user'
    ]
  }
);
await GQLBuilder.removeMutationField(
  'signUpUser',
  {
    name: 'lastName',
    inside: [
      'createUser',
      'user'
    ],
  }
);

addQueryVariable / addMutationVariable

Add variables to our query or mutation.

ParamTypeKind
operationStringRequired
variableObjectRequired
  • variable:
    • name: Variable name (String)
    • type: GraphQL variable type (String)
import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.addQueryVariable(
  'authQuery',
  {
    name: 'device',
    type: 'DEVICE_TYPE!',
  }
);
await GQLBuilder.addMutationVariable(
  'signUpUser',
  {
    name: 'phone',
    type: 'String',
  }
);

removeQueryVariable / removeMutationVariable

Remove variables from query or mutation

ParamTypeKind
operationStringRequired
variableStringRequired
import { GQLBuilder } from '@azachii/gql-generator';

await GQLBuilder.removeQueryVariable('authQuery', 'device');
await GQLBuilder.removeMutationVariable('signUpUser', 'phone');

TODO

  • addQueryFieldArg
  • addMutationFieldArg
  • removeQueryFieldArg
  • removeMutationFieldArg

MIT © Azachii