Skip to content

Commit

Permalink
Remove flask_graphql.fields, add docs to README
Browse files Browse the repository at this point in the history
  • Loading branch information
rshk committed Jul 25, 2018
1 parent c67fc48 commit 5aafca9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 47 deletions.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,101 @@ class UserRootValue(GraphQLView):
return request.user

```

### File upload support

File uploads are supported via [multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).

You can simply define a ``FileUpload`` field in your schema, and use
it to receive data from uploaded files.


Example using ``graphql-core``:

```python
from collections import NamedTuple
from graphql.type.definition import GraphQLScalarType


GraphQLFileUpload = GraphQLScalarType(
name='FileUpload',
description='File upload',
serialize=lambda x: None,
parse_value=lambda value: value,
parse_literal=lambda node: None,
)


FileEchoResult = namedtuple('FileEchoResult', 'data,name,type')


FileEchoResultSchema = GraphQLObjectType(
name='FileEchoResult,
fields={
'data': GraphQLField(GraphQLString),
'name': GraphQLField(GraphQLString),
'type': GraphQLField(GraphQLString),
}
)


def resolve_file_echo(obj, info, file):
data = file.stream.read().decode()
return FileEchoResult(
data=data,
name=file.filename,
type=file.content_type)


MutationRootType = GraphQLObjectType(
name='MutationRoot',
fields={
# ...
'fileEcho': GraphQLField(
type=FileUploadTestResultSchema,
args={'file': GraphQLArgument(GraphQLFileUpload)},
resolver=resolve_file_echo,
),
# ...
}
)
```


Example using ``graphene``:

```python
import graphene

class FileUpload(graphene.Scalar):

@staticmethod
def serialize(value):
return None

@staticmethod
def parse_literal(node):
return None

@staticmethod
def parse_value(value):
return value # IMPORTANT


class FileEcho(graphene.Mutation):

class Arguments:
myfile = FileUpload(required=True)

ok = graphene.Boolean()
name = graphene.String()
data = graphene.String()
type = graphene.String()

def mutate(self, info, myfile):
return FileEcho(
ok=True
name=myfile.filename
data=myfile.stream.read(),
type=myfile.content_type)
```
45 changes: 0 additions & 45 deletions flask_graphql/fields.py

This file was deleted.

10 changes: 8 additions & 2 deletions tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from graphql.type.scalars import GraphQLString
from graphql.type.schema import GraphQLSchema

from flask_graphql.fields import GraphQLFileUpload


def resolve_raises(*_):
raise Exception("Throws!")
Expand Down Expand Up @@ -39,6 +37,14 @@ def resolve_raises(*_):
}
)

GraphQLFileUpload = GraphQLScalarType(
name='FileUpload',
description='File upload',
serialize=lambda x: None,
parse_value=lambda value: value,
parse_literal=lambda node: None,
)


def to_object(dct):
class MyObject(object):
Expand Down

0 comments on commit 5aafca9

Please sign in to comment.