EasyGraphQL / easygraphql-tester

Test GraphQL queries, mutations and schemas on an easy way! 🚀
https://easygraphql.com/docs/easygraphql-tester/overview
MIT License
314 stars 34 forks source link

Support for custom scalar validation #118

Closed thebrianbug closed 5 years ago

thebrianbug commented 5 years ago

Thanks for this great library! I would like to be able to test validations on custom scalars.

If I have a schema like so

gql`
scalar EmailAddress

type Lead {
  email: EmailAddress!
}
`

And if I load a resolver for EmailAddress in my resolvers, I want the ability to test that my validator is correctly hooked up to the EmailAddress tag in the graphQl block. Notice that testing this connection is not the same as testing the EmailAddress resolver in isolation. Is there currently a way to do this? If not, can the feature be added?

thebrianbug commented 5 years ago

Disclaimer I'm relatively new to GraphQl, so I might not have used exactly the right words here. If I need to try explaining anything again let me know 😄 .

estrada9166 commented 5 years ago

Can you try to create the schema and the resolvers using this codesandbox template.

We can use this to try to reproduce what you're having and make it works :)

I think that when you use it, it might create a new url for your example, so please share with me when you have it.

Also, if you don't want to use this template, can you share with me the resolvers, please.

Thanks

thebrianbug commented 5 years ago

Thanks for the quick response! TBH the resolvers come from a 3rd party package, but I'll see what I can do to reproduce in the sandbox.

The resolver is defined like so and included in the resolvers list:

resolvers.ts

import { EmailAddress } from "graphql-scalars";

export default {
  EmailAddress,
  ...
};
thebrianbug commented 5 years ago

Link: https://www.npmjs.com/package/graphql-scalars

estrada9166 commented 5 years ago

This may work to test the custom scalars, you can find tests for the resolvers and also mock for the query.

Also, here is some code to execute it, you can convert this to some tests and expect the returned value from your resolver

const gql = require('graphql-tag')
const EasyGraphQLTester = require('../lib')
const { EmailAddress } = require("graphql-scalars");

const schema = gql`
  scalar EmailAddress

  type Lead {
    name: String!
    email: EmailAddress!
  }

  type Query {
    lead: Lead!
  }
`

const lead = (root, args, ctx) => {
  return {
    name: "demo",
    email: "demo@demo.com"
  };
};

const resolvers = {
  EmailAddress,
  Query: {
    lead
  }
};

const tester = new EasyGraphQLTester(schema, resolvers)

const query = `
  {
    lead {
      name
      email
    }
  }
`;

tester.graphql(query)
  .then(result => console.log(result))
  .catch(err => console.log(err))
thebrianbug commented 5 years ago

Here I've reproduced it. See the failing test: https://codesandbox.io/s/easygraphql-tester-template-k64wm

https://codesandbox.io/embed/easygraphql-tester-template-k64wm

thebrianbug commented 5 years ago

Got it! I see that I have to use the .graphql command to actually run the resolvers. It would be great to add a note in the documentation saying that .test doesn't actually run the resolvers, but only validates the schema. I'm ready to close this issue. Thanks for the help!

thebrianbug commented 5 years ago

In fact, I can't find any documentation on the tester.graphql() method. Might be a nice addition.

estrada9166 commented 5 years ago

Yeah, definitely. There's this documentation but maybe is not that clear.

I had been working to update the whole documentation and make it much clear.

Thanks!!