almilo / gql-tools

GraphQL Tools for schema handling.
21 stars 3 forks source link

Mocks #3

Open lamuertepeluda opened 8 years ago

lamuertepeluda commented 8 years ago

I cannot understend how to pass mock types. Could you please provide an example?

`'JSON file with mock data, defaults to the schema file name with ".json" extension.'``

is of little help

(what I'm trying to do is to mock scalar Date)

almilo commented 8 years ago

Scalars are not supported at the moment.

Although it will not work with scalars either, if you are interested in serving a schema file with mocking capabilities, you should be better off trying this instead: https://github.com/almilo/granate/tree/master/src/annotations/mock

cheers

lamuertepeluda commented 8 years ago

I see, thank you. By looking at your code, it seems an issue of graphql-tools, not yours. Scalars seem to get ignored.
But good news is, you are not tied to using JSON as mockfile, but you can define a mocks.js file as well, in plain javascript.

Here's my workaround, close to what I wanted to achieve but enough to play with GraphQL:

mocks.js

module.exports = {
    Date:{
        value: new Date()
    },
    Buffer: (new Buffer(10)).toJSON(),
    Report: {
        title: 'Report title',
        text: 'Hola mundo',
        position: {
            latitude: 45.233992837,
            longitude: 7.21876218
        }
    }
}

and schema

interface Entity {
  id: ID!
  name: String
}
type User implements Entity {
    id: ID!
    name: String
    age: Int
    balance: Float
    is_active: Boolean
    reports:[Report]
}

type Location {
    latitude: Float,
    longitude: Float
}

# scalar Date
type Date{
    value: String
}

# scalar Buffer
type Buffer{
    type: String,
    data:[Int]
}

type Report{
    id: ID!,
    date: Date,
    title: String!
    text: String!,
    position: Location,
    picture: Buffer
}

type Query {
    #query me
    me: User,
    #query user by id
    user(id: ID!): User
    #query report by id
    report(id: ID!): Report
    #query list of users
    users(limit: Int = 10): [User]!
    #query user reports
    reports(forUser: ID!, limit: Int = 5): [Report]!
}

schema {
    query: Query
}

and query

{
  report(id:"ec495aac-2bac-4ad9-b133-84798ce3332b"){
    id,
    title,
    text,
    date{
      value
    },
    picture{
      data
    }
  }
}