graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Encounter graphql-compose-relay error when upgrade to 9.0 #280

Open blueandhack opened 4 years ago

blueandhack commented 4 years ago

When I upgrade to version 9.0 The relay plugin throw error

throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) should have recordIdFn. ` + 'This function returns ID from provided object.');

This is my code

import { composeMongoose } from 'graphql-compose-mongoose'
import { composeWithRelay } from 'graphql-compose-relay'
const customizationOptions = {}

export const CreateBaseComposer = QueryModel => {
  const ModelTC = composeWithRelay(
    composeMongoose(QueryModel, customizationOptions)
  )
  return ModelTC
}
nodkz commented 4 years ago

Are you using relay classic or modern?

For Relay modern, there is no need to use graphql-compose-relay.

blueandhack commented 4 years ago

Are you using relay classic or modern?

For Relay modern, there is no need to use graphql-compose-relay.

Alright, I think I use Relay modern, I just create a simple example project to try, then it works. Thanks. BTW, for my current production project, when I upgrade to version 9, I face a lot of function names that need changes, it is too hard to upgrade the old project. But I figure out the new version has a lot of new features I desired, hahaha. 😂

farshid1 commented 3 years ago

@nodkz Let me start by saying this is an amazing library! Thank you!

I have a problem with integration of graphql-compose-relay into v9. My client is running relay experimental and I use the @refetchable directive.

@refetchable directive can only be added to fragments that are "refetchable", that is, on fragments that are on Viewer, or on Query, or on a type that implements Node (i.e. a type that has an id field).

I tried providing a recordIdFn to my mongoose type composer but since the v9 api does not support .getResolver for findById anymore hence building the schema fails. Here's the reference to the code that causes the failure. Are there any workarounds?

farshid1 commented 3 years ago

Ok so I looked at the source code a little bit more and noticed that a few other APIs are also exposed from the graphql-compose-relay library. I basically manually did what the library does for all of my TCs:

import composeWithRelay, {
  getNodeInterface,
  toGlobalId,
} from 'graphql-compose-relay';
import {schemaComposer} from 'graphql-compose';

composeWithRelay(schemaComposer.Query);

[TC1, TC2, ...].forEach((tc) => {
  tc.addInterface(getNodeInterface(schemaComposer));
  tc.addFields({
    id: {
      type: 'ID!',
      description: 'The globally unique ID among all types',
      resolve: (source) => toGlobalId(tc.getTypeName(), tc.getRecordId(source)),
    },
  });
  tc.setRecordIdFn((source) => source._id);
});

So far I was able to build the project both on the server and client sides without any problems. I will let you know if this solution does not work for some reason!

Again thanks for all of these libraries. We've been having a blast with them!