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

Docs: How to query/mutate with Sequelize? #1296

Closed
tim-soft opened this issue Jun 7, 2017 · 4 comments
Closed

Docs: How to query/mutate with Sequelize? #1296

tim-soft opened this issue Jun 7, 2017 · 4 comments

Comments

@tim-soft
Copy link

tim-soft commented Jun 7, 2017

This boilerplate comes loaded with sqlite3 and sequelize.

What are the best practices for reading/writing to this database? -- I'm not entirely sure how to go about actually using the database that RSK comes with.

Can we come up with a working example of adding sequelize/sqlite3 as context to the GraphQL middleware and resolving database requests via a Query and a Mutation?

I would offer a PR code recipe for this if I can understand the process

Edit: I got it working, although still not so sure about best practices
Add sequelize context to middleware

// server.js
import sequelize from './data/sequelize'

const graphqlMiddleware = expressGraphQL(req => ({
  schema,
  context: {
    user: req.user,
    db: sequelize,
  },
  graphiql: __DEV__,
  rootValue: { request: req },
  pretty: __DEV__,
}));

app.use('/graphql', graphqlMiddleware);

In this Type definition, I lookup id and email values from the database using the email value from the initial login request.

// UserType.js
const UserType = new ObjectType({
  name: 'User',
  fields: {
    id: {
      type: new NonNull(ID),
      resolve(parent, arts, { db, user }) {
        return db.models.User.findOne({ where: { email: user._json.email}}).then(user => {
          return user.dataValues.id;
        });
      },
    },
    email: {
      type: StringType,
      resolve(parent, arts, { db, user }) {
        return db.models.User.findOne({ where: { email: user._json.email}}).then(user => {
          return user.dataValues.email;
        });
      },
    },
  },
});
@arobotd
Copy link

arobotd commented Jun 18, 2017

I'm trying to get the db working as well and this post was very helpful, since there are no examples in the repo itself.

I can query it through the GraphiQL interface ok, but it doesn't seem like the context variable is getting passed to resolve() when I'm making a query in the actual React application.

In my /queries/ingredients.js file, the argument {db} will be null and cause an error in my application.

const ingredients = {
   getAllIngredients: {
       type: new GraphQLList(IngredientType),
       description: 'A paginated query for all the ingredients',
       resolve(parent, args, {db}) {
          return db.models.Ingredient.findAll();
       },
   },
...

results in GraphQL error: TypeError: Cannot match against 'undefined' or 'null'. at resolve in the app, but is fine through GraphiQL.

My graphqlMiddleware looks like yours, but is there something else I need to do to be able to get the db in the React app? Also, I'm using the feature/apollo branch. Thanks.

@arobotd
Copy link

arobotd commented Jun 18, 2017

I believe I have to pass the db in the context to createApolloClient() in server.js:

app.get('*', async (req, res, next) => {
   try {
      const css = new Set();

      const apolloClient = createApolloClient({
         schema,
         rootValue: { request: req },
         context: {
            db: sequelize,
         },
      });
...

It seems to be working now.

@ulani
Copy link
Member

ulani commented May 27, 2021

@tim-soft thank you very much for crating this issue! Unfortunately, we have close it due to inactivity. Feel free to re-open it or join our Discord channel for discussion.

NOTE: The main branch has been updated with React Starter Kit v2, using JAM-style architecture.

1 similar comment
@ulani
Copy link
Member

ulani commented May 27, 2021

@tim-soft thank you very much for crating this issue! Unfortunately, we have close it due to inactivity. Feel free to re-open it or join our Discord channel for discussion.

NOTE: The main branch has been updated with React Starter Kit v2, using JAM-style architecture.

@ulani ulani closed this as completed May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants