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

Added fixes to apolloAzureFunctions.js and sample functions for the GraphQL API and for GraphiQL #753

Merged
merged 4 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All of the packages in the `apollo-server` repo are released with the same versi

### vNEXT

* `apollo-server-azure-functions`: Fix non-functional Azure Functions implementation and update examples in Azure Functions' `README.md`. [PR #753](https://github.com/apollographql/apollo-server/pull/753) [Issue #684](https://github.com/apollographql/apollo-server/issues/684)
* `apollo-server-adonis`: The `Content-type` of an operation response will now be correctly set to `application/json`.

### v1.3.4
Expand Down
48 changes: 31 additions & 17 deletions packages/apollo-server-azure-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ title: Azure Functions
description: Setting up Apollo Server with Azure Functions
---

[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core) [![Build Status](https://circleci.com/gh/apollographql/apollo-cache-control-js.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-cache-control-js) [![Coverage Status](https://coveralls.io/repos/github/apollographql/apollo-server/badge.svg?branch=master)](https://coveralls.io/github/apollographql/apollo-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#slack)
[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core) [![Build Status](https://travis-ci.org/apollographql/apollo-server.svg?branch=master)](https://travis-ci.org/apollographql/apollo-server) [![Coverage Status](https://coveralls.io/repos/github/apollographql/apollo-server/badge.svg?branch=master)](https://coveralls.io/github/apollographql/apollo-server?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#slack)

This is the Azure Functions integration for the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)

## Example:
## Sample Code

```js
const server = require('apollo-server-azure-functions');
const graphqlTools = require('graphql-tools');
### GraphQL:

```javascript
const { graphqlAzureFunctions } = require('apollo-server-azure-functions');
const { makeExecutableSchema } = require('graphql-tools');

const typeDefs = `
type Random {
Expand All @@ -34,23 +36,35 @@ const resolvers = {
},
};

const schema = graphqlTools.makeExecutableSchema({
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});

module.exports = function run(context, request) {
if (request.method === 'POST') {
server.graphqlAzureFunctions({
endpointURL: '/api/graphql',
schema: schema,
})(context, request);
} else if (request.method === 'GET') {
return server.graphiqlAzureFunctions({
endpointURL: '/api/graphql',
})(context, request);
}
graphqlAzureFunctions({ schema })(context, request);
};
```

[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)
### GraphiQL

```javascript
const { graphiqlAzureFunctions } = require('apollo-server-azure-functions');

export function run(context, request) {
let query = `
{
rands {
id
rand
}
}
`;

// End point points to the path to the GraphQL API function
graphiqlAzureFunctions({ endpointURL: '/api/graphql', query })(
context,
request,
);
}
```
32 changes: 18 additions & 14 deletions packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
HttpStatusCodes,
} from 'azure-functions-typescript';
import { GraphQLOptions, runHttpQuery } from 'apollo-server-core';
import * as GraphiQL from 'apollo-server-module-graphiql';
import {
GraphiQLData,
resolveGraphiQLString,
} from 'apollo-server-module-graphiql';

export interface AzureFunctionsGraphQLOptionsFunction {
(context: IHttpContext): GraphQLOptions | Promise<GraphQLOptions>;
Expand All @@ -30,8 +33,8 @@ export interface IHeaders {

export interface AzureFunctionsGraphiQLOptionsFunction {
(context: IHttpContext, request: IFunctionRequest):
| GraphiQL.GraphiQLData
| Promise<GraphiQL.GraphiQLData>;
| GraphiQLData
| Promise<GraphiQLData>;
}

export function graphqlAzureFunctions(
Expand Down Expand Up @@ -61,13 +64,14 @@ export function graphqlAzureFunctions(
return runHttpQuery([httpContext, request], queryRequest)
.then(gqlResponse => {
const result = {
status: 200,
headers: { 'Content-Type': 'application/json' },
status: HttpStatusCodes.OK,
headers: {
'Content-Type': 'application/json',
},
body: gqlResponse,
isRaw: true,
};

httpContext.res = result;

httpContext.done(null, result);
})
.catch(error => {
Expand All @@ -78,7 +82,6 @@ export function graphqlAzureFunctions(
};

httpContext.res = result;

httpContext.done(null, result);
});
};
Expand All @@ -96,22 +99,23 @@ export function graphqlAzureFunctions(
*/

export function graphiqlAzureFunctions(
options: GraphiQL.GraphiQLData | AzureFunctionsGraphiQLOptionsFunction,
options: GraphiQLData | AzureFunctionsGraphiQLOptionsFunction,
) {
return (httpContext: IHttpContext, request: IFunctionRequest) => {
const query = request.query;

GraphiQL.resolveGraphiQLString(query, options, httpContext, request).then(
resolveGraphiQLString(query, options, httpContext, request).then(
graphiqlString => {
httpContext.res = {
status: 200,
const result = {
status: HttpStatusCodes.OK,
headers: {
'Content-Type': 'text/html',
},
body: graphiqlString,
isRaw: true,
};

httpContext.done(null, httpContext.res);
httpContext.res = result;
httpContext.done(null, result);
},
error => {
httpContext.res = {
Expand Down