js-to-graphql
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

typed-graphqlify

Build Typed GraphQL Query in TypeScript. Better TypeScript + GraphQL experience.

Install

yarn add typed-graphqlify

Example

First, define GraphQL-like JS Object:

import { graphqlify, types } from 'typed-graphqlify'
 
const getUserQuery = {
  getUser: {
    user: {
      __params: { id: 1 },
      id: types.number,
      name: types.string,
      bankAccount: {
        id: types.number,
        branch: types.string,
      },
    },
  },
}

Note that we use our types helper to define types in the result.

Then, convert the JS Object to GraphQL (string) with graphqlify:

const gqlString = graphqlify('query', getUserQuery)
 
console.log(gqlString)
// =>
//   query getUser {
//     user {
//       id
//       name
//       isActive
//       bankAccount {
//         id
//         name
//       }
//     }
//   }

Finally, execute the GraphQL:

// We would like to type this!
const result: typeof getUser = executeGraphql(graphqlify('query', getUser))
 
// As we cast `result` to `typeof getUser`,
// Now, `result` type looks like this:
// interface result {
//   user: {
//     id: number
//     name: string
//     bankAccount: {
//       id: number
//       branch: string
//     }
//   }
// }

image

TODO

  • Publish to NPM
  • Enum support
  • Variable Input support

Thanks

Inspired by https://github.com/kadirahq/graphqlify

Readme

Keywords

none

Package Sidebar

Install

npm i js-to-graphql

Weekly Downloads

2

Version

0.0.1

License

MIT

Unpacked Size

403 kB

Total Files

21

Last publish

Collaborators

  • acro5piano