azachii-gql-generator
v0.0.32
Published
Build a gql query easy and avoid typo and schema errors.
Downloads
1
Readme
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.
|Param|Type|Kind| |---|---|---| |client|String or ApolloClient|Required| |queryFile|String|Optional| |mutationFile|String|Optional|
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.
|Param|Type|Kind| |---|---|---| |fileName|String|Required|
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.
|Param|Type|Kind| |---|---|---| |name|String|Required| |variables|Object or Array|Optional| |fields|String, Object or Array| Optional|
- variables:
- Object:
- name: String (Required)
- type: String, must contain a valid GraphQLType (Required)
- Array: must contain objects with same structure as above
- Object:
- 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.
|Param|Type|Kind| |---|---|---| |name|String|Required|
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.
|Param|Type|Kind| |---|---|---| |operationName|String|Required| |field|String or Object|Required|
- 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.
|Param|Type|Kind| |---|---|---| |operationName|String|Required|
|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.
|Param|Type|Kind| |---|---|---| |operation|String|Required| |variable|Object|Required|
- 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
|Param|Type|Kind| |---|---|---| |operation|String|Required| |variable|String|Required|
import { GQLBuilder } from '@azachii/gql-generator';
await GQLBuilder.removeQueryVariable('authQuery', 'device');
await GQLBuilder.removeMutationVariable('signUpUser', 'phone');
TODO
- addQueryFieldArg
- addMutationFieldArg
- removeQueryFieldArg
- removeMutationFieldArg
MIT © Azachii