Code Monkey home page Code Monkey logo

Comments (6)

adamkl avatar adamkl commented on July 26, 2024 1

Looks like the cause (for me at least) was a breaking change in the graphql-binding API in v2.

This worked with v1.2.5:

binding.mutation.createUser(args, context, info);

but throws a Cannot convert undefined or null to object error in v2.

I did some digging and it looks like the error is coming from graphql-tools as it tries to pull fragment definitions from the passed in GraphQLResolveInfo object. The problem is, the object its trying to pull fragments from is actually the context object, which has no fragments, and so graphql-tools throws the error as it tries to iterate over the keys of an undefined object.

Version 2 of graphql-binding changed the API, and now the context has to be passed in differently. This is what works now:

binding.mutation.createUser(args, info, { context });

If you upgraded to v2 of graphql-binding and didn't update your code, you are likely passing in context in place of info.

from graphql-binding.

adamkl avatar adamkl commented on July 26, 2024

I'm seeing this too.

from graphql-binding.

adamkl avatar adamkl commented on July 26, 2024

I should also point out that the generated types for the QueryMaps on the Binding class don't agree with the underlying code.

Here are the types:

export declare class Binding extends Delegate {
    query: QueryMap;
    mutation: QueryMap;
    subscription: SubscriptionMap;
    constructor({schema, fragmentReplacements, before}: BindingOptions);
    buildMethods(): any;
    buildQueryMethods(operation: QueryOrMutation): QueryMap;
    buildSubscriptionMethods(): SubscriptionMap;
}

// This interface doesn't match the implementation
export interface QueryMap {
    [rootField: string]: (args?: {
        [key: string]: any;
    }, context?: {
        [key: string]: any;
    }, info?: GraphQLResolveInfo | string) => Promise<any>;
}

And here is the implementation:

Binding.prototype.buildQueryMethods = function (operation) {
  var _this = this;
  var queryType = operation === 'query'
      ? this.schema.getQueryType()
      : this.schema.getMutationType();
  if (!queryType) {
      return {};
  }
  var fields = queryType.getFields();
  return Object.entries(fields)
      .map(function (_a) {
      var fieldName = _a[0], field = _a[1];
      return {
          key: fieldName,
          value: function (args, info, options) { // <-- This implementation doesn't match the QueryMap interface
              return _this.delegate(operation, fieldName, args, info, options);
          },
      };
  })
      .reduce(function (acc, curr) {
      return (__assign({}, acc, (_a = {}, _a[curr.key] = curr.value, _a)));
      var _a;
  }, {});
};

So even though my code now works, TypeScript is complaining.

from graphql-binding.

divyenduz avatar divyenduz commented on July 26, 2024

We deployed a release today that makes graphql codegen more robust.

Please install the new cli via npm install -g graphql-cli and try the graphql codegen command.

Note that with prisma-binding 1.x, we need to use graphql prepare
and with prisma-binding 2.x, we need to use graphql codegen

Documentation for codegen.

Thanks!

from graphql-binding.

peterpetre avatar peterpetre commented on July 26, 2024

binding.mutation.createUser(args, info, { context });

ok this works, thanks 👍

should i close it or let it for the typescript issue?

from graphql-binding.

divyenduz avatar divyenduz commented on July 26, 2024

Thanks everyone involved for the detailed reproductions.

Closing this one as the original issue is resolved. Please open a separate issue in case something is still unresolved. Thanks.

from graphql-binding.

Related Issues (20)

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.