简体   繁体   中英

How to resolve 2 different methods in one function on Apollo GraphQL resolver?

I have this resolver

SLAccount: (_, { imsUserId, imsToken }, context) =>
  new Promise((resolve, reject) => {
    addAuth(context, imsUserId, imsToken);
    context.model
      .getUserAccount(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);
  }),

But there is a another method/model I need to include there:

  getHardware(endpoint) {
    return this.connector
      .get(
        `${endpoint}/...`,
      )
      .catch(res => this.handleError(res));
  }

So I'll need something like this:

SLAccount: (_, { imsUserId, imsToken }, context) =>
  new Promise((resolve, reject) => {
    addAuth(context, imsUserId, imsToken);
    context.model
      .getUserAccount(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);

    context.model
      .getHardware(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);
  }),

The thing is that I don't need to call them at the same time, sometimes I need just one of those methods/models inside SLAccount .

So what would be the best way to handle this?

GraphQL is driven by requirements (query).

When you need account just query for that. When you need account and related hardware just query for both - additional resolver will be called automatically. This is a basic idea behind graphQL. Look for any tutorial covering relations in graphQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM