Skip to content

Commit

Permalink
Azure Functions now returns correct response body (#753)
Browse files Browse the repository at this point in the history
* Added fixes to apolloAzureFunctions.js and sample functions for the GraphQL API and GraphiQL

* Added issue and pr details to changelog

* Maintain use of `context.done()`, but use `isRaw` instead.

Per the Azure documentation regarding the response object available at:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#response-object

* Switch Azure example to use CommonJS rather than native ESM exports.
  • Loading branch information
michael-golfi authored and abernix committed Apr 17, 2018
1 parent c87cce9 commit 3dc9719
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
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

0 comments on commit 3dc9719

Please sign in to comment.