Coder Social home page Coder Social logo

graphql-mask's Introduction

graphql-mask

CI npm package npm downloads

Graphql Mask is a simple utility for removing everything in a query (or its variables) that is not defined in a schema. Use it by passing in an arguments object containing the schema to mask against, the query to be masked, and optionally, the variables to be masked:

const { maskedQuery, maskedVariables } = graphqlMask({
  schema,
  query,
  variables
});

Usage

$ npm install graphql-mask
# or
$ yarn add graphql-mask

Filtering a query:

const graphqlMask = require("graphql-mask");
// const graphqlMask = require("graphql-mask/es5"); if you need to use this in a browser

const { maskedQuery } = graphqlMask({
  schema: `
    type Query {
      something: String!
      somethingElse: Int
    }
  `,
  query: `
    query ExampleQuery {
      something
      somethingElse
      somethingNotInSchema
    }
  `}
})

console.log(maskedQuery)

This will print...

query ExampleQuery {
  something
  somethingElse
}

Since GraphQL 14 now supports the extension of input types, you can now use grapqhl-mask to filter input variables as well:

const { maskedQuery, maskedVariables } = graphqlMask({
  schema: `
    type Query {
      something: String!
    }

    type Mutation {
      mutateSomething(something: SomethingInput): SomethingOutput
    }

    input SomethingInput {
      thisThing: String
    }

    type SomethingOutput {
      thisThing: String
    }
  `,
  query: `
    mutation ExampleMutation($something: SomethingInput) {
      mutationSomething(something: $something) {
        thisThing
        thatThing
      }
    }
  `,
  variables: {
    something: {
      thisThing: "Apple",
      thatThing: "Orange"
    }
  }
});

console.log(maskedQuery);
console.log(maskedVariables);

This will print...

mutation ExampleMutation($something: SomethingInput) {
  mutationSomething(something: $something) {
    thisThing
  }
}

and

{
  something: {
    thisThing: "Apple",
  }
}

Deprecated usage

To support filtering of both query and input variables, the following usage has been deprecated as of v0.1.0. This method of invoking graphql-mask is still supported, but wil result in warning messages.

const result = graphqlMask(
  `
  type Query {
    something: String!
    somethingElse: Int
  }
`,
  `
  query ExampleQuery {
    something
    somethingElse
    somethingNotInSchema
  }
`
);

console.log(result);

This will print...

query ExampleQuery {
  something
  somethingElse
}

graphql-mask's People

Contributors

adamkl avatar brysgo avatar dependabot[bot] avatar github-actions[bot] avatar greenkeeper[bot] avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

graphql-mask's Issues

Clear out empty selections to make sure the query is always valid

There are times when all the fields are filtered out from a selection, if that is the case, we should drop the selection. If the root is empty, we should return undefined.

  • remove empty selections
  • remove non-leaf fields with no selection
  • remove operations with no selection
  • remove fragments with no selections

An in-range update of uglify-js is breaking the build 🚨

The devDependency uglify-js was updated from 3.5.13 to 3.5.14.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

uglify-js is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: Your tests failed on CircleCI (Details).

Release Notes for v3.5.14

 

Commits

The new version differs by 2 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Issue with Type in Session State Graphql Server, not in Core Graphql Server

I'm using this package with a Session State Graphql Server.

On the Session State Server, I've defined a mutation, which does not exist on the Core/Main graphql Server.

When I hit the mutation on the session state server, in NoUnknownInputTypes.js Line 20 (if (!context._schema._typeMap[type.name.value]) {) returns true, meaning it cannot find the incoming Mutation Type coming in in the typemap, since the typemap is from the the Core/Main graphql Server and not the Session State Graphql Server.

Line 23 (noUnknownInputTypeMessage(node.name.value, type),) will error out referencing the value member, since node.name in this situation is undefined.

Example :

Node { kind: 'OperationDefinition',
  operation: 'mutation',
  name: undefined,

Changing Line 23 from noUnknownInputTypeMessage(node.name.value, type), to noUnknownInputTypeMessage(type.name.value, type), allows the Mutation to work, without errors in graphql.

Try removing custom empty selection validation

Since graphql-js#1092 was fixed, we may be able to get away with removing the custom validation I added to check for empty selection sets. Make sure the tests still pass and maybe make a new one if it makes sense.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organiszation’s settings page, under Installed GitHub Apps.

An in-range update of graphql is breaking the build 🚨

The devDependency graphql was updated from 14.3.0 to 14.3.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

graphql is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: Your tests failed on CircleCI (Details).

Release Notes for v14.3.1

14.3.1 (2019-05-23)

Bug Fix 🐞

Polish 💅

19 PRs were merged

Internal 🏠

15 PRs were merged

Committers: 2

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

TypeError: Cannot read property 'kind' of null when masking input types

We're seeing an issue when we pass a query/mutation that uses variables based on input types that don't exist in the schema being used to mask the query. (see referenced PR for unit tests)

TypeError: Cannot read property 'kind' of null

      29 |   var incrementalAst = parse(query);
      30 |   var errors;
    > 31 |   while ((errors = validate(astSchema, incrementalAst, rules)).length > 0) {
      32 |     console.log(errors);
      33 |     var nodes = [].concat.apply(
      34 |       [],

      at typeFromAST (node_modules/graphql/utilities/typeFromAST.js:32:16)
      at typeFromAST (node_modules/graphql/utilities/typeFromAST.js:37:17)
      at TypeInfo.enter (node_modules/graphql/utilities/TypeInfo.js:157:54)
      at Object.enter (node_modules/graphql/language/visitor.js:363:16)
      at visit (node_modules/graphql/language/visitor.js:254:26)
      at visitUsingRules (node_modules/graphql/validation/validate.js:74:22)
      at validate (node_modules/graphql/validation/validate.js:59:10)
      at graphqlMask (src/index.js:31:20)

I've console.logged the validation errors and printouts of the interim AST as graphql-mask iterates on the queries added in the unit tests, and it looks like the query gets into a strange state that graphql can't validate on the following pass.

  console.log src\index.js:32
    [ GraphQLError {
        message: 'Unknown type "FizzFilter".',
        locations: [ [Object] ],
        path: undefined },
      GraphQLError {
        message: 'Cannot query field "fizz" on type "Query".',
        locations: [ [Object] ],
        path: undefined } ]

  console.log src\index.js:60
    query getFizz($filter: null!) <-- wha?


  console.log src\index.js:32
    [ GraphQLError {
        message: 'Unknown type "FizzInput".',
        locations: [ [Object] ],
        path: undefined },
      GraphQLError {
        message: 'Cannot query field "fizz" on type "Mutation".',
        locations: [ [Object] ],
        path: undefined } ]

  console.log src\index.js:60
    mutation fizzer($data: null!) <-- wha?

Error when masking entire query + variables

When masking a query (with variables) where none of the queried properties exist in the provided schema, graphql-mask tries to parse an empty query resulting in the following error:

TypeError: Must provide Source. Received: null

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.