Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get params type? #52

Open
dohooo opened this issue Feb 26, 2019 · 8 comments
Open

How to get params type? #52

dohooo opened this issue Feb 26, 2019 · 8 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@dohooo
Copy link

dohooo commented Feb 26, 2019

like title...

@acro5piano
Copy link
Owner

acro5piano commented Feb 26, 2019

Hi @zwh1666258377, thank you for your interest in this project!

Do you mean operation params by params right?
If so, currently there are no ways to define and get type of params.

const params = { $input: 'UserInput!' }

// `typeof params` is just { $input: string }

Unlike typed-graphqlify, Apollo Codegen creates input type, so adding the feature seems good.

We possibly create params builder like this:

import { graphqlify, ParamsBuilder, types } from 'typed-graphqlify'

const builder = new ParamsBuilder('$input', 'UserInput!', {
  name: types.string,
})
builder.toString() // => { $input: 'UserInput!' }
typeof builder.type // => { name: string }

// now, type-check is enabled
graphqlify.mutation('UpdateUser', params(builder), {
  updateUser: builder.render({ name: 'Tom' }, {
    name: types.string
  })
})

// This must throw type error
builder.render({ name: 123 }, { ... })

And the interface will look this:

class ParamsBuilder<T> {
  constructor<T> (paramName: string, typeName: string, type: T)
  toString(): string
  render(params: T): T
}

Also note #51 might affect it.

Thanks

@acro5piano acro5piano added enhancement New feature or request good first issue Good for newcomers labels Feb 26, 2019
@acro5piano
Copy link
Owner

Or, simply

import { graphqlify, types } from 'typed-graphqlify'

interface UpdateUserParams {
  name: string
}

graphqlify.mutation<UpdateUserParams>('UpdateUser', params({ name: 'Tom' }, {
  updateUser: {
    name: types.string,
  },
})

@acro5piano
Copy link
Owner

However, people query variables like

fetch('/graphql', {
  body: JSON.stringify({
    query: ...,
    variables: {
      name: 'Tom',
    },
  }),
})

@dohooo
Copy link
Author

dohooo commented Feb 27, 2019

👍

@laurensdijkstra
Copy link

laurensdijkstra commented Jun 27, 2019

Or, simply

import { graphqlify, types } from 'typed-graphqlify'

interface UpdateUserParams {
  name: string
}

graphqlify.mutation<UpdateUserParams>('UpdateUser', params({ name: 'Tom' }, {
  updateUser: {
    name: types.string,
  },
})

This is what I'm trying to do with typed-graphqlify right now, however in the output after conversion to string, the quotation marks around 'Tom' are gone, making the GraphQL syntax illegal. Any way to circumvent/fix this?

@acro5piano
Copy link
Owner

@laurensdijkstra

This issue might help you. Which version do you use?

#46

@acro5piano
Copy link
Owner

Got it.

https://github.com/acro5piano/typed-graphqlify/blob/master/src/__test__/index.test.ts#L254

Currently we expect query-level vars, so in your case you have to use rawString helper.
I would like to make an improvement that automatically remove " only if car value starts with $.

Thanks!

@laurensdijkstra
Copy link

Currently we expect query-level vars, so in your case you have to use rawString helper.

I created a helper function that recursively runs JSON.stringify() on all string values of the parameters object, whichs works nicely. Thanks!

@acro5piano acro5piano mentioned this issue Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants