Skip to content

Zero Coding, ⚡ Rapid GraphQL Sample Data Generator and API

License

Notifications You must be signed in to change notification settings

sayjava/graphql-sample

Repository files navigation

⚡ graphql-sample ⚡

Zero Coding, ⚡ Rapid GraphQL API prototyping

CI Status CI Status

Features

  • Zero coding required
  • CRUD API for each defined type in the schema definition
  • MongoDB/Hasura style filtering API
  • Use your own sample data

Quick Start

Create a type definition file and name it schema.graphql with the following content

type User {
  userid: ID # Auto-generated IDs
  firstName: String @named(as: "name_firstName")
  lastName: String @named(as: "name_lastName")

  votes: [Vote!] @relation
}

type Vote {
  voteId: Int @unique # Auto-generated IDs
  timestamp: String @named(as: "date_recent")
  total: Int @nameD(as: "random_number")

  user: User @relation
}

and in the same folder run

npx graphql-sample

The GraphQL server will be running at http://localhost:8080/graphql. It comes with an IDE to query in the browser or use HTTP POST

and you can run a sample query like

{
  findUsers(limit: 2) {
    firstName
    lastName

    votes(where: { total: { gt: 400 } }) {
      total
      timestamp
    }
  }
}

or create a new user like

mutation {
  createUsers(data: [{ userid: 2, firstName: "james", lastName: "bond" }]) {
    users {
      firstName
    }
  }
}

Usage & Options

npx graphql-sample --help

Generating Sample Data

graphql-sample uses the wonderful faker.js underneath to generate sample data for the type fields using the format @named(as: "namespace_function") where namespace can be e.g name and function can be firstName

See the full list of available faker.js functions here at faker.js namespaces and functions

namespace_function examples are: -

  • address_country
  • commerce_department
  • company_bs
  • date_past
  • finance_iban
  • internet_email
  • image_city

See faker.js namespaces and functions

Any field defined as an ID or with @unique directive will be auto-generated

Advance Usage

Relationships

You can use the @relation directive to create a relationship between two types. Each type must have at least one unique field using @unique or GraphQL ID

BYOD (bring your own data)

if you would rather use your own sample data, any json or csv file in the same directory as your schema file can be referenced in your type definition.

For example, if you have a csv file in the same folder as your schema named users.csv with the following content

id name title
1 james manager
2 bond agent

then you can reference the file in your type definition as

type User @datasource(name: "users") {
  id: ID
  name: String
  title: String
}

Links

Maintainer

Raymond Ottun