Coder Social home page Coder Social logo

graphql-custom-types's Introduction

GraphQL Custom Types

npm version Build Status Dependency Status devDependencies Status peerDependencies Status

This is a collection of custom GraphQL types that I tend to reuse quite often so I packed them into a module.

Available Types

Let me give you an overview of the available types. If you need more detail about how to use them, check schema.js in the tests folder.

Scalar

The primitive types, aka everything that may be represented as a string. The ones with parameters you need to instantiate with new and pass according parameters, the others may be used as are.

  • GraphQLEmail
  • GraphQLURL
  • GraphQLDateTime
  • GraphQLLimitedString(min, max, alphabet)
  • GraphQLPassword(min, max, alphabet, complexity)
  • GraphQLUUID

complexity default options:

{
  alphaNumeric: false,
  mixedCase: false,
  specialChars: false
}

Installation

Most likely you already will have it, but do not forget to also install graphql, since it is required as peer dependency:

npm install graphql graphql-custom-types --save

Usage

import {
  GraphQLEmail,
  GraphQLURL,
  GraphQLDateTime,
  GraphQLLimitedString,
  GraphQLPassword,
  GraphQLUUID
} from 'graphql-custom-types';

And use it in your Schema as you would use any other type.

Development

Contributions are very welcome, please feel free to submit a type. If you do so make sure there are test cases in place.

Testing

The test suite may be invoked by running:

npm run test

graphql-custom-types's People

Contributors

aliatsis avatar arjunyel avatar drew-y avatar glavin001 avatar j0k3r avatar jmcneese avatar maticrivo avatar mdlavin avatar steffenmllr avatar stylesuxx avatar timmersthomas 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  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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

graphql-custom-types's Issues

field type must be Output Type but got: Email

[ 'Error: UserEmail.address field type must be Output Type but got: Email.',

The error comes from https://github.com/graphql/graphql-js/blob/39744381d5173795d3b245dcb5d86e78bb3638fe/src/type/definition.js#L408-L410
Which comes from namedType instanceof GraphQLScalarType returning false in https://github.com/graphql/graphql-js/blob/39744381d5173795d3b245dcb5d86e78bb3638fe/src/type/definition.js#L95-L104

The issue as it pertains to this package is that graphql-custom-types has its own copy of dependency GraphQL. Thus, namedType instanceof GraphQLScalarType is checking the overall project's GraphQLScalarType versus graphql-custom-types' GraphQLScalarType instance.

Changing dependencies to peerDependencies in package.json resolves this issue and I have tested locally.

1.1.1970 midnight validates as invalid

Following query:

{
  myQuery(date: "1970-01-01T00:00:00.000Z")
}

returns:

Query error: String is not a valid date time string

However, a year later 1.1.1971 midnight works:

{
  myQuery(date: "1971-01-01T00:00:00.000Z")
}

Other working dates:

  • 1970-01-01T00:00:00.001Z - 1ms after 1.1.1970 midnight
  • 1969-12-31T23:59:59.999Z - 1ms before 1.1.1970 midnight

The reason is that Date.parse('1970-01-01T00:00:00.000Z') returns 0 and this condition then evaluates to false.

Add additional types

Hello,
It's great to provide the community with additional types for GraphQL but I see that some are missing:

  1. LimitedInt
  2. LimitedFloat
  3. LatLng

With this the collection will become complete.

peerDependency warning

Hey, thanks for this package. It's really great!

i opened a PR #19 to fix a peerDepndency warning when installing with latest version of [email protected]

npm WARN [email protected] requires a peer of graphql@^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 but none is installed. You must install peer dependencies yourself.

parser shallowing up errors

Hi it seems error are being shallow somewhere when the value of GraphQLEmail doesn't conform, when using a string instead of a email I don't get a specific error message.

mutation {
  viewer { 
    # wrong email type ( missing @ )
    createUser(email: "asdasail.com" password: "123") {
      success
    }
  }
}

just this

{
  "message": "unexpected error"
}

And I get the error on my node terminal

2017-05-18T14:17:55.881Z : ERROR : REQ : POST /graphql? : 500 : Query error: Not a valid Email address : (58eba048307ce2316004bb2c) : [Object]: {
  "stack": "GraphQLError\n    at parser (...\\node_modules\\graphql-custom-types\\lib\\factory.js:37:17)\n    at parseLiteral (...\\node_modules\\graphql-custom-types\\lib\\types.js:38:16)\n    at GraphQLCustomScalarType.parseLiteral (...\\node_modules\\graphql\\type\\definition.js:215:21)\n    at isValidLiteralValue (...\\node_modules\\graphql\\utilities\\isValidLiteralValue.js:113:26)\n    at isValidLiteralValue (...\\node_modules\\graphql\\utilities\\isValidLiteralValue.js:51:12)\n    at Object.Argument (...\\node_modules\\graphql\\validation\\rules\\ArgumentsOfCorrectType.js:40:67)\n    at Object.enter (...\\node_modules\\graphql\\language\\visitor.js:297:29)\n    at Object.enter (...\\node_modules\\graphql\\language\\visitor.js:339:25)\n    at visit (...\\node_modules\\graphql\\language\\visitor.js:229:26)\n    at visitUsingRules (...\\node_modules\\graphql\\validation\\validate.js:77:22)\n    at Object.validate (...\\node_modules\\graphql\\validation\\validate.js:62:10)\n    at doRunQuery (...\\node_modules\\graphql-server-core\\src\\runQuery.ts:115:34)\n    at ...\\node_modules\\graphql-server-core\\src\\runQuery.ts:59:39\n    at process._tickDomainCallback (internal/process/next_tick.js:135:7)",
  "message": "Query error: Not a valid Email address",
  "locations": [
    {
      "line": 3,
      "column": 23
    }
  ],
  "nodes": [
    {
      "kind": "StringValue",
      "value": "asdasail.com",
      "loc": {
        "start": 44,
        "end": 58
      }
    }
  ],
  "source": {
    "body": "mutation {\n  viewer {\n    createUser(email: \"asdasail.com\" password: \"123\") {\n      success\n    }\n  }\n}",
    "name": "GraphQL"
  },
  "positions": [
    44
  ]
}

  GraphQLError
      at parser (...\node_modules\graphql-custom-types\lib\factory.js:37:17)
      at parseLiteral (...\node_modules\graphql-custom-types\lib\types.js:38:16)
      at GraphQLCustomScalarType.parseLiteral (...\node_modules\graphql\type\definition.js:215:21)
      at isValidLiteralValue (...\node_modules\graphql\utilities\isValidLiteralValue.js:113:26)
      at isValidLiteralValue (...\node_modules\graphql\utilities\isValidLiteralValue.js:51:12)
      at Object.Argument (...\node_modules\graphql\validation\rules\ArgumentsOfCorrectType.js:40:67)
      at Object.enter (...\node_modules\graphql\language\visitor.js:297:29)
      at Object.enter (...\node_modules\graphql\language\visitor.js:339:25)
      at visit (...\node_modules\graphql\language\visitor.js:229:26)
      at visitUsingRules (...\node_modules\graphql\validation\validate.js:77:22)
      at Object.validate (...\node_modules\graphql\validation\validate.js:62:10)
      at doRunQuery (...\node_modules\graphql-server-core\src\runQuery.ts:115:34)
      at ...\node_modules\graphql-server-core\src\runQuery.ts:59:39
      at process._tickDomainCallback (internal/process/next_tick.js:135:7)

When normally you use wrong types GraphQL gives you more information. Examples using Number instead of String in password argument.

mutation {
  viewer {
    createUser(email: "[email protected]" password: 123) {
      success
    }
  }
}
{
  "errors": [
    {
      "message": "Argument \"password\" has invalid value 123.\nExpected type \"String\", found 123.",
      "locations": [
        {
          "line": 3,
          "column": 51
        }
      ]
    }
  ]
}

All I use is a simple catch error throw

resolve() {
  try {
   //  ... code
  } catch(error) {
    throw error
  }
}

Peer dependencies

Hi,

first of all, thanks for your nice plugin.

Could you please update the peer dependencies?

That would be very nice, thank you!

Greets Semy

Serialize is happy to serialize invalid URLs (and probably other invalid types too)

You can currently send down a value like 12345 for a resolver which is supposed to resolve with a URL.

Values are only tested for validity if they are sent up as variables or literals in the user's query. The server's response is not validated (hence we can reply with invalid data).

Is this the intent of the library?

Personally I don't think it's inline with GraphQL's philosophy, because you can't return a String in place of an Int, etc. Regardless, if it's the intent of this library then we should document it, otherwise we should probably fix the serialize functions.

GraphQL Error: "Query.contact(email:) argument type must be Input Type but got: Email"

I may be missing something here but I get the ffg error attempting to use the custom type GraphQLEmail:

Query.contact(email:) argument type must be Input Type but got: Email

I have the snippet below as reference:

import {
    GraphQLString
} from 'graphql';

import {
    GraphQLEmail
} from 'graphql-custom-types';

import {ContactResolvers} from '../resolvers';
import {ContactType} from '../types';

export default {
    type: ContactType,
    args: {
        email: {type: GraphQLEmail},
        phone: {type: GraphQLString}
    },
    resolve: ContactResolvers.findContact
};

I also tried the following, but didnt get an exception when I entered the invalid string test as an email arg:

import {
    GraphQLString
} from 'graphql';

import {
    GraphQLEmail
} from 'graphql-custom-types';

import {ContactResolvers} from '../resolvers';
import {ContactType} from '../types';

export default {
    type: ContactType,
    args: {
        email: {
            type: GraphQLString,
            args: {
                item: {type: GraphQLEmail}
            },
            resolve (parent, {item}) {
                return item;
            }
        },
        phone: {type: GraphQLString}
    },
    resolve: ContactResolvers.findContact
};

Question: Is there a way to reference these types in a graphql file?

Hello, I am pretty new to GraphQL and was looking at defining as much of my schemas in .gql/.graphql files as possible. I would like to use these custom types in those files. Is there a way to do this? So, for instance, I would like:

type User {
  username: String!
  email: Email!
  ...
}

Where Email would be referencing GraphQLEmail.

GraphQLPassword typescript error

In your index.d.ts file the definition for GraphQLPassword is this:

 class GraphQLPassword extends GraphQLCustomScalarType {
    constructor(min?: number, max?: number, alphabet?: boolean, complexity?: {
      alphaNumeric?: boolean,
      mixedCase?: boolean,
      specialChars?: boolean,
    });
  }

The alphabet parameter is marked as boolean. Isn't this supposed to be a string?

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.