Giter VIP home page Giter VIP logo

apollo-upload-network-interface's Introduction

apollo-upload-network-interface

UploadNetworkInterface for Apollo GraphQL Client. Adds support for multipart/form-data requests.

Used together with graphql-server-express-upload and graphql-server-koa-upload (coming soon).

Any help is appreciated!

Usage

import ApolloClient from 'apollo-client'
import createNetworkInterface from 'apollo-upload-network-interface'

const networkInterface = createNetworkInterface({
  uri: '/graphql',
})

const client = new ApolloClient({
  networkInterface
})

If you get an error like this

/node_modules/apollo-client/transport/networkInterface.js:9
import 'whatwg-fetch';
^^^^^^

SyntaxError: Unexpected token import

You need to configure you babel-loader like this

{
  test: /\.js$/,
  loader: 'babel-loader',
  exclude: /node_modules\/(?!(apollo-client)\/).*/,
  include: [
    path.resolve(__dirname, '..', 'config'),
    path.resolve(__dirname, '..', 'client'),
    path.resolve(__dirname, '..', 'node_modules', 'apollo-client'),
    //reactMdlExtraPath,
  ],
  query: {
    cacheDirectory: true,
  },
}

TODO

  • Add tests
  • Convert to typescript

apollo-upload-network-interface's People

Contributors

hribb avatar sandervanhooft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

apollo-upload-network-interface's Issues

Make it work with React Native

As @guilhermedecampo said, this does not work with RN, because it relies on FileList check, which is not available on RN.

You guys have a suggestion to make it easy/seamless using react-native?

Since we just create an object for the file-upload the check logic does not fit because it relies on FileList / File instanceof as you can see here https://github.com/HriBB/apollo-upload-network-interface/blob/master/src/UploadNetworkInterface.js#L21 and here https://github.com/HriBB/apollo-upload-network-interface/blob/master/src/UploadNetworkInterface.js#L47.

Thanks!

I don't use RN yet, so I have absolutely no idea on how to support this. @guilhermedecampo any ideas?

Apollo file upload project

@sandervanhooft @thebigredgeek if you want, I can give you admin access to all repositories for apollo file upload. You guys are very active and I am really busy ATM, so I think it's best if we work together. Maybe we can even create a new organization/repository and use lerna and typescript in the same way as graphql-server.

In any case, we should discuss some things, because I think that apollo core devs are busy with 1.0, so I guess it's up to us to create something that works. What do you guys think?

Decorate existing network interface?

Very pleased with this package. Glad I won't need to roll my own solution haha. I was wondering if we could expose a higher-order function to decorate an existing network interface? I am currently doing the following for subscriptions:

import { print } from 'graphql-tag/printer';

// quick way to add the subscribe and unsubscribe functions to the network interface
export default function addGraphQLSubscriptions(networkInterface, wsClient) {
  return Object.assign(networkInterface, {
    subscribe(request, handler) {
      return wsClient.subscribe({
        query: print(request.query),
        variables: request.variables,
      }, handler);
    },
    unsubscribe(id) {
      wsClient.unsubscribe(id);
    },
  });
}

It seems like this will be a normal pattern moving forward for extended the default apollo network interface. I don't think it would be too difficult to expose an interface for something like this, and I am willing to submit a PR if you don't have time for it.

Thoughts @HriBB ?

Allow single file uploads

Basically, allow for mutations instead of just like this:

updateAvatar(userId: String!, file: [UploadedFile!]!): Avatar

Also like this:

updateAvatar(userId: String!, file: UploadedFile!): Avatar
  • Clearer semantics.
  • Better validation for mutations requiring only one file in a variable.
  • It is sometimes easier to get a single File object to send as a mutation parameter:
handleSubmit = event => {
    event.preventDefault()
    const formData = new window.FormData(event.target)
    this.props.mutate({
      variables: {
        userId: this.props.userId,
        file: formData.get('avatar') // Instance of File (not FileList), from <input type='file' name='avatar' required />
      }
    }).then(({data}) => {
      console.log('Avatar updated', data)
    })
  }

To get this to work, handle File as well as FileList here and here.

Npm warning

When installing, I get this warning:

npm WARN deprecated @types/[email protected]: This is a stub types definition for Redux (https://github.com/reactjs/redux). Redux provides its own type definitions, so you don't need @types/redux installed!

(Which I just ignore)

Files variable type

@HriBB @thebigredgeek

What type should the files variable be when passed to the mutation?

The file upload works fine when using your example code. But when using react-dropzone or tcomb-form the files seem to be not handled appropriately.

Only works with top-level variables

We should probably support FileLists nested in variables. It's common to pass an input type rather than scalars for mutation and query params. It seems like a simple bit of recursion could make this work.

Unexpected token import

Hi, I have issue after use it.

This is the code

import ApolloClient from 'apollo-client';
import createNetworkInterface from 'apollo-upload-network-interface';


const networkInterface = createNetworkInterface({
  uri: GRAPHQL_ENDPOINT,
});

const client = new ApolloClient({
  networkInterface
});

then when run the applications this is the error.

Uncaught SyntaxError: Unexpected token import -> networkInterface.js?formatted:9


import 'whatwg-fetch'; <- here is where the error appear
import { print } from 'graphql-tag/printer';
export function printRequest(request) {

apollo-client and apollo-upload-network-interface versions here

"apollo-client": "^0.8.3",
"apollo-upload-network-interface": "^1.0.4",

thanks

GraphQL error: Variable "image" not used

I'm trying to upload a file like this:

const ATTACH_IMAGE = gql`
  mutation attachImage($entityID: Int!, $mappingName: String!, $image: UploadedFile) {
    attachImage(entityID:$entityID, mappingName:$mappingName) {
      __typename
      id
      url
    }
}`;

this.props.client.mutate({
   mutation: ATTACH_IMAGE,
   variables: {
      image: this.state.uploadedFile,
      entityID: 3,
      mappingName: "organization_background"
   }

But I get this error message:

GraphQL error: Variable "image" not used

It seems that Apollo checks the mutation and notices that the $image variable is not used anywhere, even though I need to set it if I want the apollo-upload-network-interface to take care of it. I already had this problem when I did a custom implementation of a network interface, and the only way I found is to manually remove the $image: UploadedFile bit from the mutation string, not a great solution!

Any idea of what might go wrong? Do you have a working example somewhere?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.