GraphQL Zeus – Autocomplete GraphQL Queries in JavaScript and TS

GraphQL Zeus creates autocomplete client library for Javascript or Typescript which provides autocompletion for strongly typed queries.

How it works

Given the following schema Olympus Cards

Play with it

$ zeus https://faker.graphqleditor.com/aexol/olympus/graphql ./generated
import { Api } from "./generated/graphql-zeus"

License

MIT

How to use

Main usage of graphql zeus should be as a CLI.

As a CLI

Installation

Install globally

$ npm i -g graphql-zeus

Of course you can install locally to a project and then use as a npm command or with npx

Usage with Javascript

$ zeus schema.graphql ./

It will also generate corresponding out.d.ts file so you can have autocompletion,

Usage with TypeScript

$ zeus schema.graphql ./  --ts

Load from URL

$ zeus https://faker.graphqleditor.com/aexol/olympus/graphql ./generated

Use generated client example

import { Api } from './graphql-zeus';

// This will return Card object with ID only
const createCards = async () => {
  let ZeusCard = await Api(
    'https://faker.graphqleditor.com/aexol/olympus/graphql'
  ).Mutation.addCard({
    card: {
      name: 'Zeus',
      description:
        "Sky and Thunder god, rules olympus. Hasn't devoured any children. Zeus' father, Cronus ate all of his children except for Zeus.",
      Attack: 10,
      Defense: 5,
      Children: 92 // YES!
    }
  })({
    id: true
  });
  let DionysusCard = await Api(
    'https://faker.graphqleditor.com/aexol/olympus/graphql'
  ).Mutation.addCard({
    card: {
      name: 'Dionysus',
      description:
        'Dionysus is the Greek god of wine, has the power to bring the dead back to life.',
      Attack: 1,
      Defense: 15,
      Children: 9
    }
  })({
    id: true,
    name: true
  });
  // How to call graphql functions on objects
  const [UpdatedDionysusCard, UpdatedZeusCard] = (await Api(
    'https://faker.graphqleditor.com/aexol/olympus/graphql'
  ).Query.cardById({
    cardId: DionysusCard.id
  })({
    attack: [
      {
        cardID: [ZeusCard.id]
      },
      {
        id: true,
        Attack: true,
        Children: true,
        Defense: true,
        description: true,
        name: true
      }
    ]
  })).attack;
  console.log(UpdatedDionysusCard);
  console.log(UpdatedZeusCard);
};

To run the example navigate to: ./example and run

$ npm i

then run

$ npm run start

Spec

PROMISE_RETURNING_OBJECT = Api.[OPERATION_NAME].[OPERATION_FIELD](
    ...OPERATION_FIELD_PARAMS
)(
    ...RETURN PARAMS
)

Internal graphql type functions

PROMISE_RETURNING_OBJECT = Api.[OPERATION_NAME].[OPERATION_FIELD](
    ...OPERATION_FIELD_PARAMS
)(
    ...RETURN_PARAMS
    [
        {
            ...FUNCTION_PARAMS
        },
        {
            ...RETURN_FUNCTION_PARAMS
        }
    ]
).then( object => object[FUNCTION_NAME] )

Use In your Project to generate code

This will be rarely used, but here you are!

import { Parser,TreeToTS } from 'graphql-zeus';

const schemaFileContents = `
type Query{
    hello: String!
}
schema{
    query: Query
}
`

const typeScriptDefinition = TreeToTS.resolveTree(Parser.parse(schemaFileContents));

const jsDefinition = TreeToTS.javascript(Parser.parse(schemaFileContents));

Support

Join our GraphQL Editor Channel

Contribute

For a complete guide to contributing to GraphQL Editor, see the Contribution Guide .

  1. Fork this repo
  2. Create your feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Parsing

Simplier approach to GraphQL parsing. Using graphql-js library and parsing AST to simplier types.

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章