jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

Using the scalar with template literals? #250

Closed joakimunge closed 3 years ago

joakimunge commented 3 years ago

Hey, I'm pretty new to using custom Scalars and had a question regarding this.

I'm trying to use the Scalar provided by this package, but in our projects we're using the gql template literal utility provided by apollo server to define our type defs, like so:

const { gql } = require('apollo-server')
const invoiceApprovalsTypeDefs = require('../invoice-approvals/types/invoiceApprovalTypes')

const typeDefs = gql`
  scalar JSONObject
  scalar DateTime

  enum RequestStatusType {
    ALL
    ONGOING
    SOLVED
    DELETED
  }

  type RequestsResponse {
    requests: [Request]!
    count: Int!
    pages: Int
  }

How would I go about importing the GraphQlUpload scalar and using it in the template literal?

Thanks

jaydenseric commented 3 years ago

In the GraphQLUpload scalar docs, you can see an example titled “A schema built using makeExecutableSchema from @graphql-tools/schema.”:

https://github.com/jaydenseric/graphql-upload#class-graphqlupload

In your case, you are using apollo-server so it's not exactly like the example because your provide typeDefs and resolvers to the ApolloServer constructor, but it's pretty much the same thing:

new ApolloServer({ typeDefs, resolvers });

Make sure that your typeDefs configured for Apollo Server include scalar Upload, and that the resolvers configured contain Upload: GraphQLUpload, where GraphQLUpload is imported from graphql-upload.

Once the Upload scalar configured for your schema, you should then just be able to use Upload as a type throughout your schema, just like String, etc.

For the record, I don't recommend having GraphQL SDL string typeDefs and separate JS resolvers that a tool brings together; it's better to use GraphQL.js to just compose your schema using regular JavaScript modules and then provide it to the ApolloServer constructer with the schema option, like this:

https://github.com/jaydenseric/apollo-upload-examples/blob/7cf5f7d370c4395dab7607a607b1dc786d3963b9/api/server.mjs#L25

Hope that helps :)