Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

possible to add to context? #1

Open
veeramarni opened this issue Feb 2, 2019 · 1 comment
Open

possible to add to context? #1

veeramarni opened this issue Feb 2, 2019 · 1 comment

Comments

@veeramarni
Copy link

Similar to graphql server, can we add something to the context which could be used for resolver.

For example

// resolvers -> get where on earth id -> get consolidated_weather data and return
const resolvers = {
  Query: {
    cityWeather: (root, args, { weatherCtx } , info) => {
      return weatherCtx.getWoeid(args.city_name).then( (response) => {
        if (!response) {
          return null;
        }
        return weatherCtx.getWeather(response).then( (weather) => {
          if (!weather) {
            return null;
          }
          let consolidated_weather = weather.consolidated_weather;
          // check for args applicable_date to apply filter
          consolidated_weather = args.applicable_date ? consolidated_weather.find(item => item.applicable_date === args.applicable_date) : consolidated_weather[0];
          const respObj = {'temp': consolidated_weather.the_temp.toString(), 'min_temp': consolidated_weather.min_temp.toString(), 'max_temp': consolidated_weather.max_temp.toString(), 'city_name': weather.title, 'applicable_date': consolidated_weather.applicable_date};
          return respObj;
        });
      });
    }
  },
};
@Fubinator
Copy link

Fubinator commented May 20, 2021

I'm currently facing the same problem. I'd like to inject some context into the client's query function and work with that inside the resolver.

  const schema = makeExecutableSchema({
    typeDefs,
    resolvers
  });

  const client = new ApolloClient({
    link: new SchemaLink({ schema }),
    cache: new InMemoryCache(),
  });

  const result = await client.query({ query: qry, context: "hallo" });

And the resolver:

    Query: {
      item: (root: any, args: any, context: any, info: any) => {
        console.log("ctx", context);
        return {};
      }
  }

Executing this logs undefined.

@wawhal sorry for pinging, but do you have any idea?

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

No branches or pull requests

2 participants