Coder Social home page Coder Social logo

rse / graphql-tools-types Goto Github PK

View Code? Open in Web Editor NEW
47.0 4.0 2.0 73 KB

Custom Scalar Types for GraphQL-Tools

Home Page: https://www.npmjs.com/package/graphql-tools-types

JavaScript 96.09% Makefile 3.91%
graphql graphq-tools custom scalar type

graphql-tools-types's Introduction

GraphQL-Tools-Types

Custom Scalar Types for GraphQL-Tools

About

This Node.js module provides the custom scalar types Void, Integer, Float, String, Date, Universally Unique Identifier (UUID) and JavaScript Object Notation (JSON) for GraphQL Tools, a wrapper around the GraphQL engine GraphQL.js.

Installation

$ npm install graphql-tools-types

Usage

import UUID              from "pure-uuid"
import * as GraphQL      from "graphql"
import * as GraphQLTools from "graphql-tools"
import GraphQLToolsTypes from "graphql-tools-types"

let definition = `
    schema {
        query: RootQuery
    }
    scalar Void
    scalar MyInt
    scalar MyFloat
    scalar MyString
    scalar Date
    scalar UUID
    scalar JSON
    scalar Coord
    type RootQuery {
        exampleVoid: Void
        exampleMyInt(num: MyInt): Int
        exampleMyFloat(num: MyFloat): Float
        exampleMyString(str: MyString): String
        exampleDate(date: Date): Date
        exampleUUID(uuid: UUID): UUID
        exampleJSON(json: JSON): JSON
        exampleCoord(coord: Coord): Coord
    }
`
let resolvers = {
    Void:     GraphQLToolsTypes.Void({ name: "MyVoid" }),
    MyInt:    GraphQLToolsTypes.Int({ name: "MyInt", min: 0, max: 100 }),
    MyFloat:  GraphQLToolsTypes.Float({ name: "MyFloat", min: 0.0, max: 100.0 }),
    MyString: GraphQLToolsTypes.String({ name: "MyString", regex: /^(?:foo|bar|quux)$/ }),
    Date:     GraphQLToolsTypes.Date({ name: "MyDate" }),
    UUID:     GraphQLToolsTypes.UUID({ name: "MyUUID" }),
    JSON:     GraphQLToolsTypes.JSON({ name: "MyJSON" }),
    Coord:    GraphQLToolsTypes.JSON({ name: "Coord", struct: "{ x: number, y: number }" }),
    RootQuery: {
        exampleVoid: (root, args, ctx, info) => {
            return {}
        },
        exampleMyInt: (root, args, ctx, info) => {
            return args.num
        },
        exampleMyFloat: (root, args, ctx, info) => {
            return args.num
        },
        exampleMyString: (root, args, ctx, info) => {
            return args.str
        },
        exampleDate: (root, args, ctx, info) => {
            return args.date
        },
        exampleUUID: (root, args, ctx, info) => {
            return args.uuid
        },
        exampleJSON: (root, args, ctx, info) => {
            return args.json
        },
        exampleCoord: (root, args, ctx, info) => {
            return args.coord
        }
    }
}
let schema = GraphQLTools.makeExecutableSchema({
    typeDefs: [ definition ],
    resolvers: resolvers,
})
let query = `
    query ($date: Date, $uuid: UUID, $json: JSON, $coord: Coord) {
        exampleVoid,
        exampleMyInt(num: 100),
        exampleMyFloat(num: 42.7),
        exampleMyString(str: "foo")
        date1: exampleDate(date: $date),
        date2: exampleDate(date: "2016-08-16T00:01:02.000Z"),
        uuid1: exampleUUID(uuid: $uuid),
        uuid2: exampleUUID(uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4"),
        json1: exampleJSON(json: $json),
        json2: exampleJSON(json: { foo: "bar", baz: 42, quux: true }),
        coord1: exampleCoord(coord: $coord),
        coord2: exampleCoord(coord: { x: 7, y: 42 })
    }
`
let variables = {
    date: "2016-08-16T00:01:02.000Z",
    uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4",
    json: { foo: "bar", baz: 42, quux: true },
    coord: { x: 7, y: 42 }
}
GraphQL.graphql(schema, query, null, null, variables).then((result) => {
    console.log("OK", result)
}).catch((result) => {
    console.log("ERROR", result)
})
$ npm test
OK { data:
   { exampleJSON: { foo: 'bar', baz: 42, quux: true },
     exampleUUID: '6cbe657c-63e3-11e6-aa83-080027e303e4',
     exampleDate: '2016-08-16T00:01:02.000Z',
     exampleVoid: {},
     exampleMyInt: 100,
     exampleMyFloat: 42.7,
     exampleMyString: 'foo' } }

License

Copyright (c) 2016-2021 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

graphql-tools-types's People

Contributors

rse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

graphql-tools-types's Issues

Add graphql and graphql-tools as a peerDependency.

Currently I'm trying to use graphql-tools. But the graphql packaged in express-graphql and this package seems to conflict and this is the error I'm getting.

{
  "errors": [
    {
      "message": "Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory.",
      "stack": "Error: Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory.\n    at invariant (/home/pyros2097/Code/mine/cress/node_modules/graphql/jsutils/invariant.js:19:11)\n    at validate (/home/pyros2097/Code/mine/cress/node_modules/graphql/validation/validate.js:60:72)\n    at /home/pyros2097/Code/mine/cress/packages/cress-cli/node_modules/express-graphql/dist/index.js:140:52\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:188:7)"
    }
  ]
}

So I think making this to peerDependency might help.

Add description field

Add a description field, like you can do with GraphQLScalarType

import { GraphQLScalarType } from 'graphql';

new GraphQLScalarType({
  name: 'UInt',
  description: 'A non-negative Int',
}),

float and int values

in the function __parseValue is done a parseFloat (parseInt), but e.g if the value is "1,6" everything after the comma ist lost and then 1 is a valid value -> but "1,6" at all should not be valid

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.