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

Allow to configure the rootValue #30

Closed
ggrossetie opened this issue Dec 6, 2022 · 4 comments
Closed

Allow to configure the rootValue #30

ggrossetie opened this issue Dec 6, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request released Has been released and published

Comments

@ggrossetie
Copy link

Story

As a user migrating from express-graphql, I want to pass a rootValue resolver. With express-graphql, I was able to do the following:

app.get('/graphql', graphqlHTTP((req, res) => ({
  schema,
  rootValue: resolvers
})))

Ideally, I would like to set rootValue as an option of createHandler

app.use(
  '/graphql',
  createHandler({ schema,  rootValue }),
)

Acceptance criteria

  • user is able to configure a rootValue without overriding the whole ExecutionContext with onSubscribe
  • user can find documentation on how to configure a rootValue

Note

Maybe using a rootValue is not recommended anymore? Maybe there's an alternative? For reference, here's how I declare my schema/resolvers:

module.exports = {
  /**
   * Create an article.
   */
  articles: async (args, { req }) => {
    // ...
  }
  /**
   * Delete an article.
   */
  deleteArticle: async (args, { req }) => {
    // ...
  },
}
const gql = require('graphql-tag')
const { buildASTSchema } = require('graphql')

module.exports = buildASTSchema(gql`
  type Article {
    _id: ID!
    title: String
    content: String
    createdAt: String
    updatedAt: String
  }

  type RootQuery {
    articles (user: ID): [Article!]!
  }

  type RootMutation {
    deleteArticle(article: ID!): Article!
  }

  schema {
    query: RootQuery
    mutation: RootMutation
  }
`)
@enisdenjo
Copy link
Member

This makes sense! Thank you for opening an issue, I will tackle this rather soon.

@enisdenjo enisdenjo self-assigned this Dec 16, 2022
@enisdenjo enisdenjo added the enhancement New feature or request label Dec 16, 2022
enisdenjo pushed a commit that referenced this issue Dec 24, 2022
# [1.10.0](v1.9.0...v1.10.0) (2022-12-24)

### Features

* **handler:** Accept a GraphQL execution rootValue ([0f04fa2](0f04fa2)), closes [#30](#30)
@enisdenjo
Copy link
Member

🎉 This issue has been resolved in version 1.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@enisdenjo enisdenjo added the released Has been released and published label Dec 24, 2022
@manishbhatt94
Copy link

@enisdenjo There isn't much documentation available on how to use rootValue.

Like in the samples shared in the issue's description by this issue's author, we get access to query arguments and the request object as the 1st & 2nd parameters of each resolver defined via rootValue in express-graphql.

But in graphql-http, that isn't the case, I think, since graphql-http when calling the resolvers passed undefined as the 2nd argument instead of { req }.

@manishbhatt94
Copy link

Hey, I figured out how it works.. you need to use the context option in createHandler (which has access to the request) and then return whatever you want, and that would be passed as the 2nd argument when the library calls the resolvers. Sorry for posting the earlier comment, but the thing is the documentation is kinda not straightforward in the sense of its not mentioned anywhere with what arguments will resolvers defined under rootValue be called with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released Has been released and published
Projects
None yet
Development

No branches or pull requests

3 participants