strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.86k stars 511 forks source link

Mocking for testing queries/mutations #1380

Open basvandriel opened 2 years ago

basvandriel commented 2 years ago

from here

Upvote & Fund

Fund with Polar

aryaniyaps commented 2 years ago

Thanks for opening this issue! It would be helpful to add more information on this, so that we could understand the problem at hand and help out accordingly! 😄

basvandriel commented 2 years ago

I mean when defining a schema, allow a option for mocking data. For example, there is a query returning a array of numbers, without defining a resolver, the array will be randomized.

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
    resolved: String
  }
`;

const resolvers = {
  Query: {
    resolved: () => 'Resolved',
  },
};

const mocks = {
  Int: () => 6,
  Float: () => 22.1,
  String: () => 'Hello',
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  mocks,
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`)
}); 
aryaniyaps commented 2 years ago

I think this could be best implemented as a plugin, and @jkimbo already suggested this!

jkimbo commented 2 years ago

For context here is a proof of concept of how mocking could work: https://replit.com/@jkimbo/BiodegradableInfiniteModem#main.py

And here is the discord thread where we discussed it: https://discord.com/channels/689806334337482765/890981673041289227/890991139858038825