Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails if I extend GraphQLObjectType #442

Closed
Wyctus opened this issue Mar 14, 2021 · 2 comments
Closed

Fails if I extend GraphQLObjectType #442

Wyctus opened this issue Mar 14, 2021 · 2 comments
Assignees
Labels

Comments

@Wyctus
Copy link

Wyctus commented Mar 14, 2021

Description

If I create a GraphQL type the following way, then everything works as expected:

const Author: GraphQLObjectType = new GraphQLObjectType({
        name: 'Author',
        extensions: {
            joinMonster: {
                sqlTable: 'authors',
                uniqueKey: 'id',
            }
        },
        fields: () => ({
            id: {
                type: GraphQLInt
            },
            user: {
                type: GraphQLInt
            },
            name: {
                type: GraphQLString
            },
            profile_photo: {
                type: GraphQLInt
            },
            description: {
                type: GraphQLString
            }
        })
});

But for some specific reason, I have to extend the GraphQLObjectType type to abstact away a couple of things:

class Model extends GraphQLObjectType {
    constructor() {
        super({
            name: 'Author',
            extensions: {
                joinMonster: {
                    sqlTable: 'authors',
                    uniqueKey: 'id',
                }
            },
            fields: () => ({
                id: {
                    type: GraphQLInt
                },
                user: {
                    type: GraphQLInt
                },
                name: {
                    type: GraphQLString
                },
                profile_photo: {
                    type: GraphQLInt
                },
                description: {
                    type: GraphQLString,
                }
            })
        });
    }
}

Now if I use the new Model class instead of the GraphQLObjectType then join-monster fails, since it relies on the constructor.name to recognize tables, as you can see here.
But in my case, due to extending the class, constructor.name becomes Model, so it wont recognize it as GraphQLObjectType anymore.

A possible solution is to check if the parent is a GraphQLObjectType by something like: Object.getPrototypeOf(instance.constructor) === GraphQLObjectType.
(But this should be done recursively, to support multilevel inheritance.)

Is there any workaround which can help in my usecase?

Environment

  • Version of this library used: 3.0.1
@nicoabie
Copy link
Contributor

nicoabie commented May 5, 2024

@nicoabie
Copy link
Contributor

You can do it like this graphql/graphql-js#3266

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants