2017-12-11 23:11:11 -08:00
|
|
|
---
|
|
|
|
title: Azure Functions
|
|
|
|
description: Setting up Apollo Server with Azure Functions
|
|
|
|
---
|
2017-08-15 09:59:29 +02:00
|
|
|
|
2018-04-17 11:39:06 -07:00
|
|
|
[](https://badge.fury.io/js/apollo-server-core) [](https://travis-ci.org/apollographql/apollo-server) [](https://coveralls.io/github/apollographql/apollo-server?branch=master) [](https://www.apollographql.com/#slack)
|
2017-12-11 23:11:11 -08:00
|
|
|
|
|
|
|
This is the Azure Functions integration for the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)
|
2017-08-15 09:59:29 +02:00
|
|
|
|
2018-04-17 11:39:06 -07:00
|
|
|
## Sample Code
|
2017-08-15 09:59:29 +02:00
|
|
|
|
2018-04-17 11:39:06 -07:00
|
|
|
### GraphQL:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const { graphqlAzureFunctions } = require('apollo-server-azure-functions');
|
|
|
|
const { makeExecutableSchema } = require('graphql-tools');
|
2017-08-15 09:59:29 +02:00
|
|
|
|
|
|
|
const typeDefs = `
|
|
|
|
type Random {
|
|
|
|
id: Int!
|
|
|
|
rand: String
|
|
|
|
}
|
|
|
|
|
|
|
|
type Query {
|
|
|
|
rands: [Random]
|
|
|
|
rand(id: Int!): Random
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
const rands = [{ id: 1, rand: 'random' }, { id: 2, rand: 'modnar' }];
|
2017-08-15 09:59:29 +02:00
|
|
|
|
|
|
|
const resolvers = {
|
|
|
|
Query: {
|
|
|
|
rands: () => rands,
|
2018-01-09 00:08:01 +01:00
|
|
|
rand: (_, { id }) => rands.find(rand => rand.id === id),
|
|
|
|
},
|
2017-08-15 09:59:29 +02:00
|
|
|
};
|
|
|
|
|
2018-04-17 11:39:06 -07:00
|
|
|
const schema = makeExecutableSchema({
|
2017-08-15 09:59:29 +02:00
|
|
|
typeDefs,
|
2018-01-09 00:08:01 +01:00
|
|
|
resolvers,
|
2017-08-15 09:59:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = function run(context, request) {
|
2018-04-17 11:39:06 -07:00
|
|
|
graphqlAzureFunctions({ schema })(context, request);
|
2017-08-15 09:59:29 +02:00
|
|
|
};
|
|
|
|
```
|
2018-03-27 09:22:13 -07:00
|
|
|
|
2018-04-17 11:39:06 -07:00
|
|
|
### 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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|