mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

[Question] How to map array of sequelize objects to graphql objects? #557

Closed skkap closed 6 years ago

skkap commented 6 years ago

I have custom resolve function in which I retrieve sequelize objects from some other function. How can I map these objects into graphql objects?

{
    type: graphqlTypes.User,
    args: {
        query: { type: new GraphQLNonNull(GraphQLString) },
    },
    async resolve(root, args) {
        try {
            const users = await getUsers(query)
            return ???
        } catch (error) {
            ...
        }
    },
}
mickhansen commented 6 years ago

Your type definition is not a list, it should be if you want to be able to resolve an array.

skkap commented 6 years ago

@mickhansen Oh yeah, that is right. Thank you. It is just example. The actual question is, how to map this list of Sequelize Users to GraphQL ones? I am still looking for the solution.

mickhansen commented 6 years ago

The usual way, graphql built-in resolvers just do object[key], so that usually works if your fields are named the same as your sequelize attributes.

skkap commented 6 years ago

@mickhansen I was under the impression there should be some helper for that... Is there any example?

mickhansen commented 6 years ago

Helper for what part? There's a resolver helper, it's in the readme.

skkap commented 6 years ago

@mickhansen Resolver performs sequelize DB request by itself. I receive sequelize entities from another service, which has complicated logic which is really difficult to define in before callback of the resolver. I would like to implement resolve method myself, but use graphql-sequelize only for mapping of sequelize entities to graphql entities. Is it possible?

May be I don't fully understand how graphql-sequelize works, but it should map sequelize entities to graphql entities after performing request in resolver, right?

mickhansen commented 6 years ago

graphql-sequelize does not map sequelize objects to graphql types in resolvers no. sequelize instances simply just work with the default graphql field resolver. if you implement resolve() yourself you generally won't need graphql-sequelize

skkap commented 6 years ago

@mickhansen thank you for the clarification! I still can use it for attributeFields method to define GraphQL types though.